IPhone motion control

I have one code that moves uiimage to touch-event

This is true:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[[event allTouches]anyObject];
touchOffset= image.center.x-[touch locationInView:touch.view].x;
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[[event allTouches]anyObject];
//paddle movemant coordinates
float distanceMoved=([touch locationInView:touch.view].x+touchOffset)- image.center.x;
float newX= image.center.x+distanceMoved;
if(newX>30 &&newX<290)
image.center=CGPointMake(newX, image.center.y);
if(newX>290)
image.center=CGPointMake(290, image.center.y);
if(newX<30)
image.center=CGPointMake(30,  image.center.y);
}

BUT the problem is that I want this action to be performed when the button is pressed (left-right). When I pressed the left button, UIImage moves a little to the left. And when ever I pressed the right button UIImage moved a little to the right side, basically I want the image motion controller with the button only on the x axis of the iphone or pad.

early

+3
source share
2 answers

CGRect frame = [image frame]; frame.origin.x + = 10.0f; [image setFrame: frame]; use it and match your actions with it.

0
source
CGRect frame = [image frame];
frame.origin.x += 10.0f;
[image setFrame:frame];
+1
source

Source: https://habr.com/ru/post/1763064/


All Articles