iOS核心动画CoreAnimation系统进阶(转场动画&组动画)

本文详细介绍了如何使用Core Animation中的CATransition进行转场动画,包括设置动画类型、进度等,并演示了如何创建复杂的动画组合,如贝塞尔曲线动画、关键帧动画和颜色变化动画。此外,还展示了如何利用动画组简化动画代码。

CATransition

这里写图片描述

核心代码

//转场动画:默认淡入淡出
    CATransition *anim =[CATransition animation];
    anim.type =@"suckEffect";//从父视图的左上角收缩
    anim.startProgress =.5;//从动画进程的一半开始
    anim.startProgress =.8;//从动画进程的百分之八十结束
    [_imgView.layer addAnimation:anim forKey:nil];

动画组

在不使用动画组的情况下

//绘制贝塞尔曲线
    UIBezierPath *path =[UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 500)];
    [path addCurveToPoint:CGPointMake(350, 500) controlPoint1:CGPointMake(170, 400) controlPoint2:CGPointMake(220, 300) ];
    //添加到layer
    CAShapeLayer * shapeLayer =[CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = nil;
    shapeLayer.strokeColor =[UIColor orangeColor].CGColor;
    [self.view.layer addSublayer:shapeLayer];

    //添加灰色方块
    CALayer * colorLayer =[CALayer layer];
    colorLayer.frame = CGRectMake(0, 0, 30, 30);
    colorLayer.position = CGPointMake(50, 500);
    colorLayer.backgroundColor =[UIColor grayColor].CGColor;
    [self.view.layer addSublayer:colorLayer];

    //添加关键帧动画
    CAKeyframeAnimation * anim=[CAKeyframeAnimation animation];
    anim.path = path.CGPath;
    anim.keyPath =@"position";
    anim.duration = 3;
    [colorLayer addAnimation:anim forKey:nil];

    //添加基础动画-动态改变颜色
    CGFloat red =arc4random() /(CGFloat)INT_MAX;
    CGFloat green =arc4random() /(CGFloat)INT_MAX;
    CGFloat blue =arc4random() /(CGFloat)INT_MAX;
    CABasicAnimation * basicAnim =[CABasicAnimation animation];
    UIColor * color =[UIColor colorWithRed:red green:green blue:blue alpha:1];
    basicAnim.keyPath =@"backgroundColor";
    basicAnim.toValue = (id)color.CGColor;
    basicAnim.duration = 3;
    [colorLayer addAnimation:basicAnim forKey:nil];

    //改变大小
    CABasicAnimation * anim1 =[CABasicAnimation animation];
    anim1.keyPath = @"transform.scale";
    anim1.toValue =@.2;
    anim1.duration = 3;
    [colorLayer addAnimation:anim1 forKey:nil];

使用动画组

CAAnimationGroup * group =[CAAnimationGroup animation];
    group.animations = @[anim1,anim,basicAnim];
    group.duration = 3;
    [colorLayer addAnimation:group forKey:nil];

优点是我们没必要写一些重复的属性.

效果图
这里写图片描述

gitHub代码示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值