我有一个通过以下代码创建的 UIButton。
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)];
当点击选择器按钮时,调用buttonAction方法,并通过360度旋转动画对按钮进行动画处理,如下:
- (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"];
}
它的工作很好,但这不是我真正想要的。我希望当我的按钮被点击时,按钮将以"reload_selected.png"图像(我设置为UIControlStateSelected)为背景,然后运行动画。当动画完成后,按钮必须返回到其实际背景("refresh_icon.png")。
有人可以建议一些代码更改来帮助我实现上述操作吗?感谢您提前的帮助。