我有一段代码,可以在触摸事件中移动UIImage。
代码如下:
-(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);
}
但问题在于,我希望这个操作必须在按钮(左右按钮)动作上执行。每当我按下左侧按钮时,UIImage会稍微向左移动一点。每当我按下右侧按钮时,UIImage会稍微向右移动一点。基本上,我希望通过按钮控制图像只在iPhone或iPad的x轴上移动。感谢您的帮助。