Change background image and UIButton animation when using iOS 7

I have a UIButton created using the following code

button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"refresh_icon.png"] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"reload_selected.png"] forState:UIControlStateSelected]; [button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside]; [button setFrame:CGRectMake(0, 0, 20 , 20)]; 

Here, when he pressed the selectorAction button, I am also animating the button 360 degrees of rotation, like this

 - (void) buttonAction : (id) sender { NSLog(@"Reload Button Clicked"); [self runSpinAnimationOnView:self.button duration:1.0 rotations:1.0 repeat:0]; } - (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat; { CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 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"]; } 

It works well, but that’s not what I really want. I want that by clicking on my button, the button will take the image "reload_selected.png" (which I set for UIControlStateSelected) as a background, and then start the animation. When the animation is finished, the button will again return to the current background ("refresh_icon.png").

Can someone suggest some changes to the code that will help me achieve the above action? Thanks in advance for your help.

+7
ios objective-c uibutton core-animation
source share
2 answers

Can you try this

 [UIView animateWithDuration:YOURDURATION delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^ { [button setImage:[UIImage imageNamed:@"reload_selected.png"] forState:UIControlStateSelected]; button.transform = CGAffineTransformRotate(CGAffineTransformIdentity,M_PI * 2); } completion:^(BOOL finished) { [button setImage:[UIImage imageNamed:@"refresh_icon.png"] forState:UIControlStateNormal]; } ]; 
+1
source share

Instead of using Core Animation, you can use the animateWithDuration:animations:completion: method of the UIView . This method allows you to specify the level of completion lock that you can use to reset the image after the animation finishes.

EDIT: Do something like

 [view animateWithDuration:duration animations:^{ //Do your animations here } completion: ^(BOOL finished){ if(finished) { //reset the image } }]; 

To specify an animation, you can animate the view transform property using CGAffineTransformRotate .

0
source share

All Articles