ios - 点击 iOS 7 时更改背景图像并为 UIButton 设置动画

标签 ios objective-c uibutton core-animation

<分区>

我有一个由以下代码创建的 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”)。

任何人都可以建议对代码进行一些更改以帮助我实现上述操作吗?预先感谢您的帮助。

最佳答案

你能不能这样试试,

[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];
                         }
    ];

关于ios - 点击 iOS 7 时更改背景图像并为 UIButton 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922533/

相关文章:

ios - 进行实时 HTTP 流式传输时显示 AVPlayerItem 状态未知

ios - Pod Install 在 Xcode 项目的终端中抛出错误

ios - 呈现 SKScene 后取消隐藏 UIButtons 时延迟?

objective-c - CocoaPods 不为 iOS7 和 Xcode 5 安装 Pod

ios - 如何像 Foursquare 一样在 UITableView 中放置一个按钮 "over"

ios - 按钮文字不显示

ios - 在 ScrollView subview 中添加自定义 UIButton subview 会导致 CUICatalog : Invalid asset name supplied

ios - malloc: *** 错误: 已释放对象的校验和不正确 - 对象可能在被释放后被修改

iphone - 关闭屏幕播放声音/不要让 iPhone 进入休眠状态

ios - 将 cornerRadius 和 setbackgroundimage 设置为 UIButton