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 * 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.
ios objective-c uibutton core-animation
ayon
source share