#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_btn.layer removeAnimationForKey:@"rotation"];
CABasicAnimation *animation = [[CABasicAnimation alloc] init];
animation.fromValue = @(0);
animation.toValue = @(M_PI * 2);
animation.duration = 30;
animation.keyPath = @"transform.rotation.z";
animation.repeatCount = NSIntegerMax;
animation.removedOnCompletion = NO;
[_btn.layer addAnimation:animation forKey:@"rotation"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(UIButton *)sender {
// 更改按钮的播放状态
sender.selected = !sender.selected;
if (sender.selected) {
[sender setTitle:@"暂停" forState:UIControlStateNormal];
[self pauseAnimate];
} else {
[sender setTitle:@"开始" forState:UIControlStateNormal];
[self resumeAnimate];
}
}
//暂停动画
- (void)pauseAnimate
{
CFTimeInterval pausedTime = [_btn.layer convertTime:CACurrentMediaTime() fromLayer:nil];
_btn.layer.speed = 0.0;
_btn.layer.timeOffset = pausedTime;
}
//恢复动画
- (void)resumeAnimate
{
CFTimeInterval pausedTime = [_btn.layer timeOffset];
_btn.layer.speed = 1.0;
_btn.layer.timeOffset = 0.0;
_btn.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [_btn.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
_btn.layer.beginTime = timeSincePause;
}









网友评论