- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 90, 30, 30)];
? ? _imageView.image= [UIImageimageNamed:@"011.png"];
? ? [self.viewaddSubview:_imageView];
}
//長按圖片抖動效果的實現(xiàn)
- (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];
? ? //將之前創(chuàng)建的點移除,每次點擊的時候只有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"];
}