- (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{
//取得动画
CABasicAnimation *animation = (CABasicAnimation*)[_imageView.layer animationForKey:@"animation1"];
if(animation ==nil) {
[self rotationAction];
}else{
if(_imageView.layer.speed==0) {
[selfstartAnimation];
}else{
[self pauseAnimation];
}
}
}
//暂停动画c
-(void)pauseAnimation{
//0---3 1.5 取得当前动画停止的时间
CFTimeInterval pauseTime = [_imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
_imageView.layer.timeOffset= pauseTime;
_imageView.layer.speed=0;
}
//开始动画
-(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; //一直重复
[_imageView.layer addAnimation:basicAnimationforKey:@"animation1"];
}
//缩放动画
-(void)scaleAnimation{
CABasicAnimation*basicAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];
basicAnimation.fromValue=@0;
basicAnimation.toValue=@2;
basicAnimation.duration=0.5;
//下面两步保持动画结束后的状态不变
// basicAnimation.removedOnCompletion = NO;
// basicAnimation.fillMode = kCAFillModeForwards;
//在恢复之前状态过程自动添加动画
basicAnimation.autoreverses=YES;
//添加动画
[_imageView.layeraddAnimation:basicAnimationforKey:nil];
}