1

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

我的问题是

  1. 如何使用核心动画执行此任务?
  2. 在我的解决方案中,当标签数大于 20 时,动画非常缓慢。为什么?
-(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"];
}

我的解决方案对吗?能告诉我如何更合适地执行此方法吗?

4

1 回答 1

0

而不是使用手势尝试使用UITouch. 它将作为 2 个事件有所帮助,touchesBegan并将touchesMoved包括您的CABasicAnimation方法。

于 2012-07-27T05:29:51.767 回答