Error in Button Jiggle algorithm

I implemented button animation for animation. Here the button click sways, but the problem is that the animation does not stop and gets an error in [self.layer removeAllAnimations]; below is the code;

-(IBAction)button1Clicked:(id)sender { UIButton *no1 =sender; output= [self answerCheck:no1.titleLabel.text]; self.label.text=output; [self enableOptions:NO]; [self loadingView]; [self startJiggling:2]; } - (void)startJiggling:(NSInteger)count { CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? +1 : -1 ) )); CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? -1 : +1 ) )); CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -kAnimationTranslateX, -kAnimationTranslateY); CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform); self.btnOption1.transform = leftWobble; // starting point [UIView animateWithDuration:0.1 delay:(count * 0.08) options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ self.btnOption1.transform = conCatTransform; } completion:nil]; [self stopJiggling]; } -(void)stopJiggling { [self.btnOption1.layer removeAllAnimations]; self.btnOption1.transform = CGAffineTransformIdentity; // Set it straight } 
+4
source share
2 answers

You set the animation to self.btnOption1 , so you need to remove it from self.btnOption1 :

 - (void)stopJiggling { [self.btnOption1.layer removeAllAnimations]; self.btnOption1.transform = CGAffineTransformIdentity; } 

but in fact, if you just set the transform property of the button again, outside the animation block, it will remove the animation:

 - (void)stopJiggling { self.btnOption1.transform = CGAffineTransformIdentity; } 

(This worked in my test project.)

Update:

I notice that you start the animation with a delay, and you call stopJiggling immediately after calling animateWithDuration:... I do not know why you are using delay or why you are calling stopJiggling right away.

I created a test case to match your code:

 @implementation ViewController { __unsafe_unretained IBOutlet UIButton *btnOption1; } - (IBAction)startJiggling { btnOption1.transform = CGAffineTransformMakeRotation(-.1); [UIView animateWithDuration:.1 delay:2 * 0.08 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ btnOption1.transform = CGAffineTransformMakeRotation(.1); } completion:nil]; [self stopJiggling]; } - (void)stopJiggling { [btnOption1.layer removeAllAnimations]; btnOption1.transform = CGAffineTransformIdentity; } @end 

I connected my btnOption1 ivar to the button and connected the button to the startJiggling method. With the code, as shown in the figure, pressing the button does nothing, because the animation is deleted immediately after it is added. If I comment on the removeAllAnimations post, clicking the button causes the button to start to tremble, and it twitches forever. I tested the iPhone 4.3 simulator, iPhone 5.0 simulator, iPhone 5.1 simulator and my iPhone 4S with iOS 5.1.

So, I could not reproduce your problem. Submitting removeAllAnimations removes the animation in my test.

I suspect that you just want the animation to repeat twice and then stop (since you have an argument called count and you pass 2). If this is what you want to do, you can do it like this:

 - (IBAction)startJiggling { btnOption1.transform = CGAffineTransformMakeRotation(-.1); [UIView animateWithDuration:.1 delay:2 * 0.08 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ [UIView setAnimationRepeatCount:2]; btnOption1.transform = CGAffineTransformMakeRotation(.1); } completion:^(BOOL completed){ btnOption1.transform = CGAffineTransformIdentity; }]; } 

You set the repeat counter inside the animation block with +[UIView setAnimationRepeatCount:] , and you restore the button transformation in the completion block.

+4
source

Remember to import QuartzCore / QuartzCore.h to talk to layers.

0
source

All Articles