gpt4 book ai didi

objective-c - 通过触摸移动多个对象

转载 作者:行者123 更新时间:2023-11-29 11:12:21 26 4
gpt4 key购买 nike

我有一个应用程序,您必须在其中移动不同的字母(以 UILabels 的形式)触摸。由于有许多不同的 UILabel 对象,我尝试创建此代码,以防止任何标签粘在一起:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];

CGPoint touchPoint = [touch locationInView: self.view];


bool pickup = YES;


if (pickup) {

if (CGRectContainsPoint(a.frame, touchPoint)) {

a.center = touchPoint;

pickup = NO;
}
}

if (pickup) {

if (CGRectContainsPoint(x.frame, touchPoint)) {

x.center = touchPoint;
pickup = NO;

}

}

if (pickup) {

if (CGRectContainsPoint(x2.frame, touchPoint)) {

x2.center = touchPoint;
pickup = NO;

}

}
if (pickup) {

if (CGRectContainsPoint(eq.frame, touchPoint)) {

eq.center = touchPoint;
pickup = NO;

}

}

if (pickup) {

if (CGRectContainsPoint(b.frame, touchPoint)) {

b.center = touchPoint;
pickup = NO;

}

}


}

但是这里有一些问题:

  1. 移动不流畅,一旦我的手指移动图像标签就停止移动(显然是因为 if (CGRectContainsPoint(a.frame, touchPoint))

  2. 而且一旦我在移动另一个标签时绕过一个标签,我的手指就会开始移动我绕过的标签

我该怎么做,我知道有比我现在正在做的更好的方法...

最佳答案

尝试将其全部放在一个链式动画中 touchesMoved 是老派。在每个标签上添加一个手势识别器。然后在那个选择器中尝试这个 -

- (void)labelTouchSelector:(UIGestureRecognizer *)gesture
{
CGPoint touchPoint = [gesture locationInView: self.view];

[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
if(CGRectContainsPoint(a.frame, touchPoint))
{
a.center = touchPoint;
pickup = NO;
}
}
completion:^(BOOL finished)
{
if(finished)
{
//do some cleanup here
pickup = NO;
}
}];
return;
}

关于objective-c - 通过触摸移动多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10958029/

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