这是我的代码
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
UITouch *myTouch2 = [[event allTouches] anyObject];
UITouch *myTouch3 = [[event allTouches] anyObject];
button.center = [myTouch locationInView:self.view];
button2.center = [myTouch2 locationInView:self.view];
button3.center = [myTouch3 locationInView:self.view];
}
问题是,当我尝试移动其中一个按钮时,它们都会在同一时间和相同位置移动。我希望能够单独和自由地移动按钮。我怎么做?有什么建议吗?
答案 0 :(得分:1)
使用以下内容确定它是哪个按钮:
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(button.frame, location))
{
button.center = [myTouch locationInView:self.view];
}
else if(CGRectContainsPoint(button2.frame, location))
{
button2.center = [myTouch locationInView:self.view];
}
else if(CGRectContainsPoint(button3.frame, location))
{
button3.center = [myTouch locationInView:self.view];
}
答案 1 :(得分:0)
我希望能够单独和自由地移动按钮。
然后你不应该同时将它们全部移到同一个地方:
UITouch *myTouch = [[event allTouches] anyObject];
UITouch *myTouch2 = [[event allTouches] anyObject];
UITouch *myTouch3 = [[event allTouches] anyObject];
查看myTouch
,myTouch2
和myTouch3
- 它们可能都指向同一个触摸对象。在这种情况下,您将把所有按钮移动到同一位置。
如果您期待多次触摸,则需要查看[event allTouches]
中的触摸设置,并确保为每个按钮获取不同的对象。例如,您可以选择最接近每个按钮的触摸对象,确保不要对两个按钮使用相同的触摸。