Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop UIView CABasicAnimation in iPhone

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

like image 856
Hari Babu Avatar asked May 02 '13 11:05

Hari Babu


1 Answers

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];
like image 143
Bhupendra Avatar answered Oct 02 '22 00:10

Bhupendra