我有一个代码可以在触摸事件上移动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或pad的x轴上。
提前致谢
答案 0 :(得分:1)
CGRect frame = [image frame];
frame.origin.x += 10.0f;
[image setFrame:frame];
答案 1 :(得分:0)
CGRect frame = [image frame]; frame.origin.x + = 10.0f; [image setFrame:frame]; 使用这个&amp;用它来映射你的按钮动作。