How to stop UIView CABasicAnimation in iPhone

Hari Babu picture Hari Babu · May 2, 2013 · Viewed 14.3k times · Source

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

- (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...

Answer

Bhupendra picture Bhupendra · May 2, 2013

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:

[yourView.layer removeAllAnimations];