Objective c 通过触摸移动多个对象

Objective c 通过触摸移动多个对象,objective-c,ios,touch,uilabel,Objective C,Ios,Touch,Uilabel,我有一个应用程序,你必须用触摸来移动不同的字母(以ui标签的形式)。由于存在许多不同的UILabel对象,因此我尝试创建此代码,以防止任何标签粘在一起: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchPoint = [touch locationInView: self.view];

我有一个应用程序,你必须用触摸来移动不同的字母(以
ui标签的形式)。由于存在许多不同的
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;

    }

}


}
但这里有一些问题:

  • 移动不是平滑的,一旦我的手指移动了图像,标签就会停止移动(显然是因为
    if(CGRectContainsPoint(a.frame,touchPoint))
    

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


  • 我知道有一种比我现在做的更好的方法,我该怎么做…

    试着把所有的东西都放在一个链接动画中
    touchMoved
    是老派的。在每个标签上添加一个手势识别器。然后在该选择器中尝试-

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

    我将尝试创建一个新的UILabel类,该类将实现touchesBegind、touchesMoved和toucheSend。将相关代码放入每个方法中。为此类创建一个方法,为成员分配字母值。在ViewController中实例化该类的每个新对象,并在此处设置其字母以及其他必要的参数详细信息,如label.center。我希望这有帮助

    我的代码中的示例(使用UIImage,您需要找到一种使用UILabel初始化类的方法,但是标准方法会这样做):


    嗯…我什么时候调用这个方法,而且,你不能在一个块中分配拾取…在你的手势识别器中添加这个方法作为选择器,你分配给每个UILabel。嗯…我必须收回它不幸的是…我留下了我的旧代码,所以我认为它可以工作。什么都没有发生。。。
    @implementation myClass
    
    - (id)initWithImage:(UIImage *)image
    {
        if (self = [super initWithImage:image])
            self.userInteractionEnabled = YES;
    
        return self;
    }
    
    
    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch * touch = [touches anyObject];
        pos = [touch locationInView: self];
    
    
        self.center = pos;
    
    
            NSLog(@"Touches Began Called.");
    
    }
    
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
        activePoint = [[touches anyObject] locationInView:self];    
    
        int dx = self.center.x + (activePoint.x - pos.x); 
        int dy = self.center.y + (activePoint.y - pos.y);
    
        CGPoint newPoint = CGPointMake(dx,dy);
        self.center = newPoint;
    
            NSLog(@"Touches Moved Called.");
    
    
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    
    
        NSLog(@"Touches Ended Called.");
    
    }