iOS CAKeyframeAnimation关键帧动画
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 90, 30, 30)];
_imageView.image= [UIImageimageNamed:@"011.png"];
[self.viewaddSubview:_imageView];
}
//长按图片抖动效果的实现
- (IBAction)longPressAction:(id)sender {
UILongPressGestureRecognizer *press = (UILongPressGestureRecognizer *) sender;
if (press.state == UIGestureRecognizerStateBegan ) {
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
keyFrameAnimation.duration=0.1;
keyFrameAnimation.repeatCount=MAXFLOAT;
keyFrameAnimation.autoreverses=YES;
CGFloat angle = M_PI_4/8;
keyFrameAnimation.values=@[@(angle),@(-angle)];
UIImageView*imageView = (UIImageView*)press.view;
[imageView.layer addAnimation:keyFrameAnimationforKey:nil];
}
}
//点击开始时调用
- (void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{
CGPoint p = [[touchesanyObject]locationInView:self.view];
[self moveAnimation:p];
[self movePathArc:p];
[self movePathStroke:p];
}
//曲线路径运动
-(void)movePathStroke:(CGPoint)point{
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL, point.x , point.y);
// CGPathAddCurveToPoint(path, NULL, 20, 200, 120, 20, 300, 200);
// CGPathAddCurveToPoint(path, NULL, 120, 190, 30, 320, 150, 200);
// CGPoint p1 = [self creatPoint];
// CGPoint p2 = [self creatPoint];
//
// CGPathAddQuadCurveToPoint(path, NULL, p1.x, p1.y, p2.x, p2.y);
CGPathAddQuadCurveToPoint(path, NULL, 10, 300, 300, 0);
keyFrameAnimation.duration=3;
keyFrameAnimation.repeatCount=MAXFLOAT;
keyFrameAnimation.autoreverses=YES;
keyFrameAnimation.path= path;
[_imageView.layeraddAnimation:keyFrameAnimationforKey:nil];
CGPathRelease(path);
}
//点击后以点击的点为圆形转动
-(void)movePathArc:(CGPoint)point {
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path,NULL, point.x, point.y,100,0,M_PI*2,0);
keyFrameAnimation.duration=2;
keyFrameAnimation.repeatCount=MAXFLOAT;
keyFrameAnimation.path= path;
[_imageView.layeraddAnimation:keyFrameAnimationforKey:nil];
CGPathRelease(path);
}
//生成随机点的方法
-(CGPoint)creatPoint{
NSInteger x = arc4random_uniform(KScreenWidth);
NSInteger y = arc4random_uniform(KScreenHeight);
CGPointpoint =CGPointMake(x, y);
returnpoint;
}
//移动动画
-(void)moveAnimation:(CGPoint)point{
// CAKeyframeAnimation *animation = (CAKeyframeAnimation *)[_imageView.layer animationForKey:@"keyFrame"];
CAKeyframeAnimation * keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// CGPoint p1 = CGPointMake(30, 30);
// CGPoint p2 = CGPointMake(370, 10);
// NSValue *value1 = [NSValue valueWithCGPoint:p1];
// NSValue *value3 = [NSValue valueWithCGPoint:p2];
NSValue*value2 = [NSValuevalueWithCGPoint:point];
//将之前创建的点移除,每次点击的时候只有21个点
NSMutableArray *_data = [[NSMutableArray alloc] init ];
for(inti =0; i <20; i ++) {
CGPointp = [selfcreatPoint];
NSValue*value = [NSValuevalueWithCGPoint:p];
[_dataaddObject:value];
}
//停留在点击的位置
keyFrameAnimation.removedOnCompletion=NO;
keyFrameAnimation.fillMode= kCAFillModeForwards;
//动画时间
keyFrameAnimation.duration=5;
//添加元素,点击的点
[_dataaddObject:value2];
keyFrameAnimation.values= _data;
[_imageView.layer addAnimation:keyFrameAnimationforKey:@"keyFrame"];
}