如何使用 Objective-C 移动多个对象
How can I move multiple objects with Objective-C
这是我的代码
-(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];
}
问题是,当我尝试移动其中一个按钮时,它们都在同一时间和同一位置移动。我希望能够单独自由地移动按钮。我怎么做?有什么建议吗?
使用以下内容确定它是哪个按钮:
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];
}
I want to be able to move buttons separately and freely.
那么你不应该同时把它们全部移动到同一个地方:
UITouch *myTouch = [[event allTouches] anyObject];
UITouch *myTouch2 = [[event allTouches] anyObject];
UITouch *myTouch3 = [[event allTouches] anyObject];
看看 myTouch
、myTouch2
和 myTouch3
-- 它们可能都指向同一个触摸对象。在这种情况下,您会将所有按钮移动到同一位置。
如果您希望进行多次触摸,则需要查看 [event allTouches]
中的一组触摸,并确保为每个按钮获得不同的对象。例如,您可以选择最靠近每个按钮的触摸对象,确保您不会对两个按钮使用相同的触摸。
这是我的代码
-(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];
}
问题是,当我尝试移动其中一个按钮时,它们都在同一时间和同一位置移动。我希望能够单独自由地移动按钮。我怎么做?有什么建议吗?
使用以下内容确定它是哪个按钮:
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];
}
I want to be able to move buttons separately and freely.
那么你不应该同时把它们全部移动到同一个地方:
UITouch *myTouch = [[event allTouches] anyObject];
UITouch *myTouch2 = [[event allTouches] anyObject];
UITouch *myTouch3 = [[event allTouches] anyObject];
看看 myTouch
、myTouch2
和 myTouch3
-- 它们可能都指向同一个触摸对象。在这种情况下,您会将所有按钮移动到同一位置。
如果您希望进行多次触摸,则需要查看 [event allTouches]
中的一组触摸,并确保为每个按钮获得不同的对象。例如,您可以选择最靠近每个按钮的触摸对象,确保您不会对两个按钮使用相同的触摸。