- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 150, 150)];
? ? _imageView.image= [UIImageimageNamed:@"0.JPG"];
? ? [self.viewaddSubview:_imageView];
}
-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event{
? ? //取得動(dòng)畫(huà)
? ? CABasicAnimation *animation = (CABasicAnimation*)[_imageView.layer? animationForKey:@"animation1"];
? ? if(animation ==nil) {
? ? ? ? [self rotationAction];
? ? }else{
? ? ? ? if(_imageView.layer.speed==0) {
? ? ? ? ? ? [selfstartAnimation];
? ? ? ? }else{
? ? ? ? [self pauseAnimation];
? ? ? ? }
? ? }
}
//暫停動(dòng)畫(huà)c
-(void)pauseAnimation{
? ? //0---3? 1.5? 取得當(dāng)前動(dòng)畫(huà)停止的時(shí)間
? CFTimeInterval? pauseTime =? [_imageView.layer? convertTime:CACurrentMediaTime() fromLayer:nil];
? ? _imageView.layer.timeOffset= pauseTime;
? ? _imageView.layer.speed=0;
}
//開(kāi)始動(dòng)畫(huà)
-(void)startAnimation{
? ? CFTimeInterval? time =CACurrentMediaTime() -? _imageView.layer.timeOffset;
? ? _imageView.layer.beginTime= time;
? ? _imageView.layer.speed=1;
? ? _imageView.layer.timeOffset = 0;
}
- (void)rotationAction{
?? ? CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
? ? basicAnimation.fromValue=0;
? ? basicAnimation.toValue=@ (M_PI*2);
? ? basicAnimation.duration=3;
? ? basicAnimation.repeatCount=HUGE_VALF;? //一直重復(fù)
? ? [_imageView.layer? addAnimation:basicAnimationforKey:@"animation1"];
}
//縮放動(dòng)畫(huà)
-(void)scaleAnimation{
? ? ? ? CABasicAnimation*basicAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];
? ? basicAnimation.fromValue=@0;
? ? basicAnimation.toValue=@2;
? ? basicAnimation.duration=0.5;
? ? //下面兩步保持動(dòng)畫(huà)結(jié)束后的狀態(tài)不變
? ? //? ? basicAnimation.removedOnCompletion = NO;
? ? //? ? basicAnimation.fillMode = kCAFillModeForwards;
? ? //在恢復(fù)之前狀態(tài)過(guò)程自動(dòng)添加動(dòng)畫(huà)
? ? basicAnimation.autoreverses=YES;
? ? //添加動(dòng)畫(huà)
? ? [_imageView.layeraddAnimation:basicAnimationforKey:nil];
}