CAAnimationGroup-倒入垃圾桶实例

// {      

    IBOutlet UIImageView *_imageView;

    

    IBOutlet UIImageView *_bushImageView;

    }

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    _imageView =[[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 76, 76)];

    [_imageView setImage:[UIImage imageNamed:@"c"]];

    [self.view addSubview:_imageView];

    //设置圆角

    _imageView.layer.cornerRadius =10;

    //设置剪裁

    _imageView.layer.masksToBounds =YES;

    //打开_imageView点击事件

    _imageView.userInteractionEnabled =YES;

    UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(selec)];

    [_imageView addGestureRecognizer:longPress];

}

- (void)selec{

    [self addBtn];

    //创建动画数组

    CAAnimationGroup *group =[CAAnimationGroup animation];

    CAAnimation *animation0=[self rotationAnimation];

    CAAnimation *animation1 =[self rotationAnimation];

    group.animations =@[animation0,animation1];

    //次数

    group.repeatCount =HUGE_VALF;

    //动画时间 动画组的时间 可以和内部各个动画的时间不同

    group.duration =.2f;

    [_imageView.layer addAnimation:group forKey:@"group"];

}

//图标摇晃

- (CAKeyframeAnimation *)rotationAnimation{

    CAKeyframeAnimation *animation =[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

    animation.duration =.2f;

    double angle =M_PI_4/16.f;

    animation.values =@[@(-angle),@(angle),@(-angle)];

    return animation;

    

}

//图标位移

- (CAKeyframeAnimation *)positionAnimation{

    

    CAKeyframeAnimation *animation =[CAKeyframeAnimation animationWithKeyPath:@"transform.translation"];

    animation.duration =.2f;

    CGFloat offset =1;

    NSValue *value1 =[NSValue valueWithCGPoint:CGPointMake(-offset, -offset)];

    NSValue *value2 =[NSValue valueWithCGPoint:CGPointMake(-offset, offset)];

    NSValue *value3 =[NSValue valueWithCGPoint:CGPointMake(offset, -offset)];

    NSValue *value4 =[NSValue valueWithCGPoint:CGPointMake(offset, offset)];

    animation.values =@[value1,value2,value3,value4];

    return animation;

}

//创建删除按钮

- (void)addBtn{

    

    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame =CGRectMake(0, 0, 20, 20);

    [btn setImage:[UIImage imageNamed:@"button_icon_close"] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(del) forControlEvents:UIControlEventTouchUpInside];

    [_imageView addSubview:btn];

}

- (void)del{

    CAAnimationGroup *group =[CAAnimationGroup animation];

    group.animations =@[[self animation0],[self animation1],[self animation2]];

    group.duration =1;

    [_imageView.layer addAnimation:group forKey:@"up"];

    

}

//transform.scale缩放

- (CABasicAnimation *)animation2{

    CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"transform.scale"];

    animation.fromValue =@1;

    animation.toValue =@0;

    animation.beginTime =.75;

    animation.duration =.2f;

    //如果想让图层保持 显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为kCAFillModeForwards

    animation.removedOnCompletion =NO;

    animation.fillMode =kCAFillModeForwards;

    return animation;

}

//修改透明度 opacity

- (CABasicAnimation *)animation1{

    CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"opacity"];

    animation.fromValue =@1;

    animation.toValue =@0;

    animation.beginTime =.75;

    animation.duration =.2f;

    //如果想让图层保持 显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为kCAFillModeForwards

    animation.removedOnCompletion =NO;

    animation.fillMode =kCAFillModeForwards;

    return animation;

    

}

//抛物线动画

- (CAKeyframeAnimation *)animation0{

    //使用path属性,只能修改position 而不能用transform.translation

    CAKeyframeAnimation *animation =[CAKeyframeAnimation animationWithKeyPath:@"position"];

    //创建路径

    CGMutablePathRef path =CGPathCreateMutable();

    //移动到某一点

    CGPathMoveToPoint(path, NULL, _imageView.layer.position.x, _imageView.layer.position.y);

    //计算向上的贝塞尔曲线的终止点

    CGPoint toPoint =CGPointMake(_imageView.center.x+(_bushImageView.center.x-_imageView.center.x)/2, _imageView.center.y-100);

    //计算control点

    CGPoint cp1 =CGPointMake(_imageView.center.x+30, toPoint.y);

    //第一个贝塞尔曲线

    CGPathAddQuadCurveToPoint(path, NULL, cp1.x, cp1.y, toPoint.x, toPoint.y);

    //第二个贝塞尔曲线点

    CGPathAddQuadCurveToPoint(path, NULL, _bushImageView.center.x, cp1.y, _bushImageView.center.x, _bushImageView.center.y);

    animation.path =path;

    CGPathRelease(path);

    //设置动画时间,和动画组时间一致

    animation.duration =1;

    //设置三次修改path完成的时间点

    animation.keyTimes =@[@0,@.3,@1];

    //设置path对应的 timeingFunction

    animation.timingFunctions =@[[CAMediaTimingFunction functionWithName:@"easeOut"],[CAMediaTimingFunction functionWithName:@"easeOut"],[CAMediaTimingFunction functionWithName:@"easeIn"]];

    return animation;

    

}

原文地址:https://www.cnblogs.com/yxt9322yxt/p/4782719.html