如何在iPhone上停止UIView CABasicAnimation

16

我使用for循环动态创建了3个视图,在其中调用了以下方法进行旋转动画。

- (void)runRightSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:    (CGFloat)rotations repeat:(float)repeat;
{
  CABasicAnimation* rotationAnimation;
  rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
  rotationAnimation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
  //rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
  rotationAnimation.duration = duration;
  //rotationAnimation.cumulative = YES;
  rotationAnimation.repeatCount = repeat;

 [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

我该如何在触摸开始时停止视图的旋转动画?...... 我已经在触摸开始方法中尝试了以下代码,但它没有起作用。

如何在视图的触摸开始事件中停止旋转动画? 尝试过以下代码,但无效:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [touches anyObject];
   int viewTag=[touch view].tag;
   UIView *myView =(UIView *)[self.view viewWithTag:viewTag];
   [myView.layer removeAllAnimations];
}

请帮助我...


1
可能是 Is there a way to pause a CABasicAnimation? 的重复问题。 - Pfitz
1个回答

33

可能是因为您正在从另一层中删除动画,而不是从添加了动画的同一层中删除动画。(您正在使用错误的视图来删除动画,请再次检查。)

您可以使用以下方法从您的视图中删除动画:

[yourView.layer removeAllAnimations];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接