核心动画

核心动画

标签(空格分隔): ios进阶


Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍。也就是说,使用少量的代码就可以实现非常强大的功能。
Core Animation是直接作用在CALayer上的,并非UIView

核心动画的框架结构

此处输入图片的描述

Core Animation的使用步骤

如果不是xcode5之后的版本,使用它需要先添加QuartzCore.framework和引入对应的框架<QuartzCore/QuartzCore.h>
开发步骤:
1. 首先得有CALayer
2. 初始化一个CAAnimation对象,并设置一些动画相关属性
3. 通过调用CALayer的addAnimation:forKey:方法,增加CAAnimation对象到CALayer中,这样就能开始执行动画了
4. 通过调用CALayer的removeAnimationForKey:方法可以停止CALayer中的动画

CAAnimation

是所有动画对象的父类,负责控制动画的持续时间和速度,是个抽象类,不能直接使用,应该使用它具体的子类

属性(一些是来自CAMediaTiming协议的属性)

  • duration:动画的持续时间
  • repeatCount:重复次数,无限循环可以设置HUGE_VALF或者MAXFLOAT
  • repeatDuration:重复时间
  • removedOnCompletion:默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为kCAFillModeForwards
  • fillMode:决定当前对象在非active时间段的行为。比如动画开始之前或者动画结束之后
  • beginTime:可以用来设置动画延迟执行时间,若想延迟2s,就设置为CACurrentMediaTime()+2,CACurrentMediaTime()为图层的当前时间
  • timingFunction:速度控制函数,控制动画运行的节奏
  • delegate:动画代理

动画填充模式

kCAFillModeRemoved
这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
kCAFillModeForwards
当动画结束后,layer会一直保持着动画最后的状态
kCAFillModeBackwards 在动画开始前,只需要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始。
kCAFillModeBoth
这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态

速度控制函数(CAMediaTimingFunction)

kCAMediaTimingFunctionLinear(线性):匀速,给你一个相对静态的感觉
kCAMediaTimingFunctionEaseIn(渐进):动画缓慢进入,然后加速离开
kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
kCAMediaTimingFunctionEaseInEaseOut(渐进渐出):动画缓慢的进入,中间加速,然后减速的到达目的地。这个是默认的动画行为。

CAAnimationDelegate(动画代理方法)

- (void)animationDidStart:(CAAnimation *)anim;
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;

CALayer上动画的暂停和恢复

#pragma mark 暂停CALayer的动画
-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];

    // 让CALayer的时间停止走动
      layer.speed = 0.0;
    // 让CALayer的时间停留在pausedTime这个时刻
    layer.timeOffset = pausedTime;
}
#pragma mark 恢复CALayer的动画
-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = layer.timeOffset;
    // 1. 让CALayer的时间继续行走
      layer.speed = 1.0;
    // 2. 取消上次记录的停留时刻
      layer.timeOffset = 0.0;
    // 3. 取消上次设置的时间
      layer.beginTime = 0.0;    
    // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)
      layer.beginTime = timeSincePause;

CAPropertyAnimation

  • CAPropertyAnimation是CAAnimation的子类,也是个抽象类,要想创建动画对象,应该使用它的两个子类:CABasicAnimationCAKeyframeAnimation
  • keyPath:通过指定CALayer的一个属性名称为keyPath(NSString类型),并且对CALayer的这个属性的值进行修改,达到相应的动画效果。比如,指定@“position”为keyPath,就修改CALayer的position属性的值,以达到平移的动画效果

CABasicAnimation

属性:

fromValue:keyPath相应属性的初始值
toValue:keyPath相应属性的结束值

动画过程说明:
  • 随着动画的进行,在长度为duration的持续时间内,keyPath相应属性的fromValue渐渐地变为toValuekeyPath
  • 内容是CALayer的可动画Animatable属性
  • 如果fillMode=kCAFillModeForwards同时removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
代码示例
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 创建动画对象
    CABasicAnimation *anima = [CABasicAnimation animation];

    // 设置修改的属性
//    anima.keyPath = @"transform.scale";

    anima.keyPath = @"position";

    // 设置修改的值
    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 400)];
    // 取消反弹
    // 设置动画完成的时候不要移除
    anima.removedOnCompletion = NO;

    // 动画保存最新的效果
    anima.fillMode = kCAFillModeForwards;

    [_redView.layer addAnimation:anima forKey:nil];
}

CAKeyframeAnimation

  • CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值
属性说明
  • values:里面的元素称为“关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
  • path:可以设置一个CGPathRef、CGMutablePathRef,让图层按照路径轨迹移动。path只对CALayer的anchorPoint和position起作用。如果设置了path,那么values将被忽略
  • keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧。如果没有设置keyTimes,各个关键帧的时间是平分的
代码示例
#define angle2radion(angle) ((angle) / 180.0 * M_PI )

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 创建帧动画,多个值之间做动画
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    // 设置修改的属性
    anim.keyPath = @"position";
    //设置动画时长
    anim.duration = 1;

    // 来回抖动的动画
//    anim.values = @[@(angle2radion(-5)),@(angle2radion(5)),@(angle2radion(-5))];
    //根据path路径移动的动画
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 200)];
    [path addLineToPoint:CGPointMake(250, 500)];
    anim.path = path.CGPath;
    //动画的重复次数
    anim.repeatCount = MAXFLOAT;

    [_imageView.layer addAnimation:anim forKey:nil];
}

CAAnimationGroup

是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行

属性

  • animations:用来保存一组动画对象的NSArray
    默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间

代码示例

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    // 创建一个动画组
    CAAnimationGroup *group = [CAAnimationGroup animation];
    // 平移
    CAKeyframeAnimation *positionAnim = [CAKeyframeAnimation animation];
    positionAnim.keyPath = @"position";
    positionAnim.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 250, 500)].CGPath;
    // 缩放
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"transform.scale";
    anim.toValue = @0.1;
    // 旋转
    CABasicAnimation *rotation = [CABasicAnimation animation];
    rotation.keyPath = @"transform.rotation";
    rotation.toValue = @(M_PI * 2);

    group.animations = @[positionAnim,rotation];
    group.autoreverses = YES;
    group.repeatCount = MAXFLOAT;

    // 注意:通过设置动画组,操作动画时长
    group.duration = 2;
    [_redView.layer addAnimation:group forKey:nil];
}

CATransition

CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点

属性

  • type:动画过渡类型
  • subtype:动画过渡方向
  • startProgress:动画起点(在整体动画的百分比)
  • endProgress:动画终点(在整体动画的百分比)

代码示例

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    // UIView转场代码
//    [UIView transitionWithView:_imageView duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
//    } completion:^(BOOL finished) {
//    }];

    // 创建转场动画对象
    CATransition *anim = [CATransition animation];
    anim.duration = 2;
    // 设置转场类型
    anim.type = @"pageCurl";

    anim.startProgress = 0.5;
    anim.endProgress = 0.8;

    // 设置转场的方向,
    //    anim.subtype = kCATransitionFromLeft;

    [_imageView.layer addAnimation:anim forKey:nil];
    // 转场动画必须要和转场代码一起
}

转场动画的type

此处输入图片的描述

UIView动画函数实现转场动画

//单视图

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;

//双视图

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值