iOS核心动画之CoreAnimation

2018-03-16  本文已影响12人  沈正方

CoreAnimation简介

Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍。也就是说,使用少量的代码就可以实现非常强大的功能。
Core Animation可以用在Mac OS X和iOS平台。
Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程。
要注意的是,Core Animation是直接作用在CALayer上的,并非UIView。

CoreAnimation的结构

CoreAnimation

CAAnimation和CAPropertyAnimation都是抽象类,不直接使用,我们主要用到的是

CoreAnimation的使用步骤

XCode5之前使用CoreAnimation需要先添加QuartzCore.framework和引入对应的框架<QuartzCore/QuartzCore.h>
具体步骤:

  1. 首先得有CALayer
  2. 初始化一个CAAnimation对象,并设置一些动画相关属性
  3. 通过调用CALayer的addAnimation:forKey:方法,增加CAAnimation对象到CALayer中,这样就能开始执行动画了
  4. 通过调用CALayer的removeAnimationForKey:方法可以停止CALayer中的动画

CAAnimation简介

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

属性说明:

以下属性来自协议CAMediaTiming

CAAnimation动画代理方法

/* Delegate methods for CAAnimation. */
public protocol CAAnimationDelegate : NSObjectProtocol {

    /* Called when the animation begins its active duration. */
    @available(iOS 2.0, *)

    optional public func animationDidStart(_ anim: CAAnimation)
    /* Called when the animation either completes its active duration or
     * is removed from the object it is attached to (i.e. the layer). 'flag'
     * is true if the animation reached the end of its active duration
     * without being removed. */
    
    @available(iOS 2.0, *)
    optional public func animationDidStop(_ anim: CAAnimation, finished flag: Bool)
}

CAPropertyAnimation

CAPropertyAnimation是CAAnimation的子类,也是个抽象类,要想创建动画对象,应该使用它的两个子类:

属性说明:

CABasicAnimation

CABasicAnimation基本动画,是CAPropertyAnimation的子类
属性说明:

动画过程说明:

使用场景

let animation = CABasicAnimation(keyPath: "position.x")
animation.toValue = 300
animation.isRemovedOnCompletion = false
animation.fillMode = "forwards"
animation.repeatCount = Float.infinity
animation.autoreverses = true
animation.duration = 1
redView.layer.add(animation, forKey: "AnimationKey1")

CAKeyFrameAnimation:帧动画

关键帧动画,也是CAPropertyAnimation的子类,与CABasicAnimation的区别是:
CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值

属性说明:

使用场景:

let animation = CAKeyframeAnimation(keyPath: "transform.rotation")
let angle1 = 5 * Float.pi / 180
let angle2 = -5 * Float.pi / 180
animation.values = [angle1, angle2, angle1]
animation.repeatCount = Float.infinity
animation.duration = 1.3
imageView.layer.add(animation, forKey: "AnimationKey3")

Tips:

  • CABasicAnimation可看做是只有2个关键帧的CAKeyframeAnimation
  • 只有帧动画才能沿着路径走

CAAnimationGroup:动画组

动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行
默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间

属性说明:

使用场景:

let animation1 = CABasicAnimation(keyPath: "position.y")
animation1.toValue = 400

let animation2 = CABasicAnimation(keyPath: "transform.scale")
animation2.toValue = 0.5

// 创建动画组,用动画组包含多个动画
let group = CAAnimationGroup()
group.animations = [animation1, animation2]
group.fillMode = "forwards"
group.isRemovedOnCompletion = false

redView.layer.add(group, forKey: "group")

CATransition:转场动画

CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点
UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果

动画属性:

使用场景:

转场动画的过渡效果

类型字符串 效果说明 关键字 方向
fade 交叉淡化过渡 YES
push 新视图把旧视图推出去 YES
moveIn 新视图移到旧视图上面 YES
reveal 将旧视图移开,显示下面的新视图 YES
cube 立方体翻滚效果
oglFlip 上下左右翻转效果
suckEffect 收缩效果,如一块布被抽走 No
rippleEffect 水滴效果 No
pageCurl 向上翻页效果
pageUnCurl 向下翻页效果
cameraIrisHollowOpen 相机镜头打开效果 No
cameraIrisHollowClose 相机镜头关闭效果 No
/*
 转场动画
 */
let transition = CATransition()
transition.type = "pageCurl"
transition.subtype = "fromRight"
transition.startProgress = 0.2
transition.endProgress = 0.5
imageView.layer.add(transition, forKey: "AnimationKey5")

Tips

  • 转场代码和转场动画必须在同一个方法中,不考虑顺序(转场动画代码在前还是转场代码在前)

UIView动画和CoreAnimation动画的区别以及不同的适用场景?

不同之处:

适用场景
根据UIView动画和CoreAnimation动画的特点

相关代码点我

上一篇 下一篇

猜你喜欢

热点阅读