廢話不多說本刽,直接上代碼
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(60, 10, 222, 66);
[button setTitle:@"點(diǎn)我" forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickTest:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor=[UIColor orangeColor];
[self.view addSubview:button];
_imgV=[[UIImageView alloc]initWithFrame:CGRectMake(60, button.bottom+40, WIDTH-120, WIDTH-120)];
_imgV.image=[UIImage imageNamed:@"mohuImage.png"];
_imgV.contentMode=UIViewContentModeScaleAspectFill;
_imgV.layer.masksToBounds=YES;
_imgV.layer.cornerRadius=(WIDTH-120)/2.0;
[self.view addSubview:_imgV];
[_imgV.layer removeAnimationForKey:@"rotation"];
CABasicAnimation *animation = [[CABasicAnimation alloc] init];
animation.fromValue = @(0);
animation.toValue = @(M_PI * 2);
animation.duration = 6;
animation.keyPath = @"transform.rotation.z";
animation.repeatCount = NSIntegerMax;
animation.removedOnCompletion = NO;
[_imgV.layer addAnimation:animation forKey:@"rotation"];
- (void)clickTest:(UIButton*)button {
if (button.selected) {
[self resumeAnimate];
}else {
[self pauseAnimate];
}
button.selected=!button.selected;
}
- (void)pauseAnimate
{
CFTimeInterval pausedTime = [_imgV.layer convertTime:CACurrentMediaTime() fromLayer:nil];
_imgV.layer.speed = 0.0;
_imgV.layer.timeOffset = pausedTime;
}
- (void)resumeAnimate
{
CFTimeInterval pausedTime = [_imgV.layer timeOffset];
_imgV.layer.speed = 1.0;
_imgV.layer.timeOffset = 0.0;
_imgV.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [_imgV.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
_imgV.layer.beginTime = timeSincePause;
}