1

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. 而且,一旦我在移动另一个标签时绕过一个标签,我的手指就会开始移动我绕过的标签

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

4

2 回答 2

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;
}
于 2012-06-09T03:11:38.150 回答
0

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

我的代码中的示例(使用 UIImage,您需要找到一种使用 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.");

}
于 2012-07-01T18:21:23.340 回答