我有一个UIView包含许多UILabel。现在我长按其中一个标签,然后其他人将用动画飞到按下的标签,其他人将不断跟随我的手指位置。
我的问题是
-(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"];
}
我的解决方案是对的吗?可以告诉我如何更恰当地执行此方法?
答案 0 :(得分:0)
尝试使用UITouch
而不是使用手势。这将有助于touchesBegan
和touchesMoved
的2个事件将包含您的CABasicAnimation
方法。