动画calayers不断跟随手指位置

时间:2012-07-27 04:39:52

标签: iphone ios core-animation

我有一个UIView包含许多UILabel。现在我长按其中一个标签,然后其他人将用动画飞到按下的标签,其他人将不断跟随我的手指位置。

我的问题是

  1. 如何使用核心动画执行此任务?
  2. 在我的解决方案中,当标签数量大于20时,动画非常缓慢。为什么?
  3. -(void)viewDidLoad{
            [super viewDidLoad];
            _longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)];
            _longGesture.numberOfTouchesRequired = 1;
            [[self dragView] addGestureRecognizer:_longGesture];
    }
    - (void)longPressGestureUpdated:(UILongPressGestureRecognizer *)longPressGesture
    {
        switch (longPressGesture.state) 
        {
            case UIGestureRecognizerStateBegan:
            {
                location = [longPressGesture locationInView:self.view];
                [self startAllLayersAnimation];
                break;
            }
            case UIGestureRecognizerStateChanged:  
            case UIGestureRecognizerStateEnded:
            {
               location = [longPressGesture locationInView:self.view];
                [self startAllLayersAnimation];
                break;
            }
            default:
                break;
        }
    }
    - (void)startAllLayersAnimation
    {
        [CATransaction begin];
        for (CALayer *layer in [self labelLayers]) 
        {
            [self startAnimation:layer];
        }
        [CATransaction commit];
    }
    - (void)startAnimation:(CALayer*)layer
    {
        CGPoint now =((CALayer*)layer.presentationLayer).position;
        CABasicAnimation * cab = [CABasicAnimation animationWithKeyPath:@"position"];
        cab.delegate = self;
        cab.removedOnCompletion = NO;
        cab.fillMode = kCAFillModeForwards;
        cab.fromValue = [NSValue valueWithCGPoint:now];
        cab.toValue = [NSValue valueWithCGPoint:location];
        cab.duration = 1;
        //cab.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        [layer addAnimation:cab forKey:@"revItUpAnimation"];
    }
    

    我的解决方案是对的吗?可以告诉我如何更恰当地执行此方法?

1 个答案:

答案 0 :(得分:0)

尝试使用UITouch而不是使用手势。这将有助于touchesBegantouchesMoved的2个事件将包含您的CABasicAnimation方法。