gpt4 book ai didi

ios - 如何使用 Objective-C 移动多个对象

转载 作者:行者123 更新时间:2023-11-29 02:06:17 25 4
gpt4 key购买 nike

这是我的代码

-(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];

}

关于ios - 如何使用 Objective-C 移动多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29758747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com