我有一个由以下代码创建的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)];
这里点击选择按钮动作,我按这样的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”)。
任何人都可以建议一些代码更改,以帮助我实现上述操作吗?在此先感谢您的帮助。
答案 0 :(得分:1)
你能尝试这样吗,
[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 :(得分:0)
您可以使用animateWithDuration:animations:completion:
UIView
方法,而不是使用核心动画。此方法允许您指定一个完成块,可以在动画完成后用于重置图像。
编辑: 做点什么
[view animateWithDuration:duration animations:^{
//Do your animations here
} completion: ^(BOOL finished){
if(finished) {
//reset the image
}
}];
要指定动画,您可能希望使用transform
为视图的CGAffineTransformRotate
属性设置动画。