- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 90, 90, 90)];
_imageView.image= [UIImageimageNamed:@"joy.jpg"];
[self.view addSubview:_imageView];
}
- (void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event{
CGPointpoint = [[touchesanyObject]locationInView:self.view];
CAKeyframeAnimation*animation1 = [selfgetAniamtion1];
CAKeyframeAnimation*animation2 = [selfgetAnimation2:point];
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations=@[animation1,animation2];
animationGroup.duration=3;
animationGroup.repeatCount=MAXFLOAT;
//加速方式: 匀速:kCAMediaTimingFunctionLinear
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
[_imageView.layer addAnimation:animationGroupforKey:nil];
}
//获取抖动动画
-(CAKeyframeAnimation *)getAniamtion1{
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
keyFrameAnimation.duration=3;
keyFrameAnimation.repeatCount=MAXFLOAT;
// keyFrameAnimation.autoreverses = YES;
CGFloat angle = M_PI*2;
// keyFrameAnimation.values = @[@(angle),@(-angle) ];
keyFrameAnimation.values=@[@(angle),@0];
[_imageView.layer addAnimation:keyFrameAnimationforKey:nil];
return keyFrameAnimation;
}
-(CAKeyframeAnimation *)getAnimation2:(CGPoint )point{
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable();
CGFloatw = point.x-_imageView.center.x;
CGFloath = point.y-_imageView.center.y;
CGRect rect =CGRectMake(_imageView.center.x,_imageView.center.y, w, h);
CGPathAddRect(path,NULL, rect);
keyFrameAnimation.path= path;
keyFrameAnimation.duration=3;
returnkeyFrameAnimation;
}