如何在iPhone中停止UIView CABasicAnimation

Har*_*abu 13 iphone touch uiview cabasicanimation ios

我有3个视图通过使用for循环动态创建.在那我称为旋转动画的下面的方法

- (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"];
}
Run Code Online (Sandbox Code Playgroud)

如何在触摸开始视图中停止旋转动画?...我尝试了以下编码触摸开始方法,但它没有工作

-(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];
}
Run Code Online (Sandbox Code Playgroud)

请帮我...

Bhu*_*dra 30

这可能是因为您要从其他图层中删除动画,而不是从添加动画的同一图层中删除动画.(您使用错误的视图删除动画,请再次检查).

您可以使用它从您的View中删除动画:

[yourView.layer removeAllAnimations];
Run Code Online (Sandbox Code Playgroud)