如何在iPhone中停止UIView CABasicAnimation

时间:2021-03-09 20:23:06

I have 3 views dynamically created by using for loop. in that i called the below method for rotation animation

我有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"];
}

how can i stop the rotation animation in touch begin of view?... i tried the below coding in touch begin method but it didn't work

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

-(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];
}

please help me...

请帮帮我...

1 个解决方案

#1


25  

It may be because of you are removing animation from the other layer rather than to remove animation from the same layer you added animation on. (You are using the wrong view to remove animation, please check it again).

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

You can use this to remove animation from yourView:

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

[yourView.layer removeAllAnimations];

#1


25  

It may be because of you are removing animation from the other layer rather than to remove animation from the same layer you added animation on. (You are using the wrong view to remove animation, please check it again).

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

You can use this to remove animation from yourView:

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

[yourView.layer removeAllAnimations];