最近项目中要做一个类似抛物线的动画,如:一个删除功能,点击删除按钮,图片飞到相应的垃圾;
//
// UIView+Parabola.h
// MusicIM
//
// Created by E&P on 14-8-29.
// Copyright (c) 2014年 xdrao. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (Parabola)
-(void)parabolaToPoint:(CGPoint)point duration:(float)duration;
-(void)parabolaToPoint:(CGPoint)point duration:(float)duration scaleTo:(float)scale;
@end
//
// UIView+Parabola.m
// MusicIM
//
// Created by E&P on 14-8-29.
// Copyright (c) 2014年 xdrao. All rights reserved.
//
#import "UIView+Parabola.h"
@implementation UIView (Parabola)
-(void)parabolaToPoint:(CGPoint)point duration:(float)duration{
CAAnimation *animation = [self pathAnimationQuadCurveToPoint:point duration:duration scaleTo:1] ;
animation.delegate = self;
[self.layer addAnimation:animation forKey:@"parabola"];
self.center = point;
}
-(void)parabolaToPoint:(CGPoint)point duration:(float)duration scaleTo:(float)scale{
CAAnimation *animation = [self pathAnimationQuadCurveToPoint:point duration:duration scaleTo:scale] ;
animation.delegate = self;
[self.layer addAnimation:animation forKey:@"parabola"];
self.transform =CGAffineTransformMakeScale(scale, scale);
self.center = point;
}
- (CAAnimation*)pathAnimationQuadCurveToPoint:(CGPoint )pt duration:(float)duration scaleTo:(float)sc;
{
CGPoint orignal = self.center;
CGPoint contolPoin = CGPointZero;
CGPoint symPoint = CGPointZero;
CGPoint destPoint = pt;
contolPoin.x =130.0;// orignal.x + (destPoint.x - orignal.x)/2;
contolPoin.y =60.0;// orignal.y - (destPoint.y - orignal.y);
symPoint.x = 2* contolPoin.x - orignal.x;
symPoint.y = orignal.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,orignal.x,orignal.y);
CGPathAddQuadCurveToPoint(path,NULL,contolPoin.x ,contolPoin.y,destPoint.x,destPoint.y);
CAKeyframeAnimation *
animation = [CAKeyframeAnimation
animationWithKeyPath:@"position"];
[animation setPath:path];
[animation setDuration:duration];
// [animation setKeyTimes:@[@0,@1,@3]];
CFRelease(path);
CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = @1.0;
scaleAnimation.toValue = [NSNumber numberWithFloat:sc];
scaleAnimation.duration = duration;
CAAnimationGroup * animationGp = [CAAnimationGroup animation];
animationGp.duration = duration;
animationGp.animations = @[animation,scaleAnimation];
return animationGp;
}
-(void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"抛物线开始");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
if (flag) {
NSLog(@"抛物线完成");
[UIView animateWithDuration:0.03
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.alpha = 0.3;
}
completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
@end
使用:
- (IBAction)testAction:(UIButton *)sender {
// [CommonFunc curveMovementWithView:sender destination:CGPointMake(40, 40)];
[sender parabolaToPoint:CGPointMake(40, 40) duration:1.0 scaleTo:0.3];
}