【iOS/动画】隐式动画

隐式动画

系统默认在在 Layer 层上实现的动画,只要改变属性,系统就会自动作出默认实现的过度动画;例如:修改背景颜色红色为绿色,系统会默认有0.25秒的过渡动画。类似这样的动画为隐式动画

  • 隐式动画是在 iOS 平台创建动态用户界面的一种直接的方式,也是UIKit动画机制的基础

事务

解释什么是隐式动画。什么是事务。系统如何确定动画的类型和动画的执行时长。

CoreAnimation 基于一个假设,屏幕上显示的任何东西都是可以做动画的。动画并不需要开发者打开,相反需要开发者主动关闭,即:动画默认打开。否者它 一直存在。

当在 CALayer 上修改一个背景色时候,屏幕上并不是颜色立即改变,而是有一个0.25秒的动画将颜色平滑过渡。

import UIKit

class ViewController: UIViewController {

    private let colorLayer = CALayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        colorLayer.frame = view.bounds
        view.layer.insertSublayer(colorLayer, above: view.layer)
        let toGreenButton = UIButton(frame: CGRect(x: 100, y: 100, width: 44, height: 44))
        toGreenButton.backgroundColor = .blue
        toGreenButton.setTitle("点中", for: .highlighted)
        view.addSubview(toGreenButton)
        toGreenButton.addTarget(self, action: #selector(changeToGreen), for: .touchUpInside)

        let toPurpleButton = UIButton(frame: CGRect(x: 100, y: 154, width: 44, height: 44))
        toPurpleButton.backgroundColor = .black
        toPurpleButton.setTitle("点中", for: .highlighted)
        view.addSubview(toPurpleButton)
        toPurpleButton.addTarget(self, action: #selector(changeToPurple), for: .touchUpInside)
    }

    @objc func changeToGreen() {
        colorLayer.backgroundColor = UIColor.green.cgColor
    }

    @objc func changeToPurple() {
        colorLayer.backgroundColor = UIColor.purple.cgColor
    }
}

这其实就是所谓的隐式动画。
隐式动画:不指定任何动画的类型,仅仅改变了一个属性,然后Core Animation来决定如何并且何时去做的动画。

Core Animation在每个run loop周期中自动开始一次新的事务(run loop是iOS负责收集用户输入,处理定时器或者网络事件并且重新绘制屏幕的东西),即使你不显式的用CATransaction.begin()开始一次事务,任何在一次run loop循环中属性的改变都会被集中起来,然后做一次0.25秒的动画。

可以通过CATransaction.animationDuration()获取隐式动画的执行时长,也可以通过CATransaction.setAnimationDuration(3)设置隐式动画的时长,如下:

 CATransaction.begin()
 let during0 = CATransaction.animationDuration()
 print("during:\(during0)") // 0.25
 CATransaction.setAnimationDuration(3)
 let during = CATransaction.animationDuration()
 print("during:\(during)") // 3
 CATransaction.commit()

demo如下:

class ViewController: UIViewController {

    private let colorLayer = CALayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        colorLayer.frame = CGRect(x: 0, y: 400, width: 77, height: 77)
        view.layer.insertSublayer(colorLayer, above: view.layer)
        let toGreenButton = UIButton(frame: CGRect(x: 100, y: 100, width: 44, height: 44))
        toGreenButton.backgroundColor = .blue
        toGreenButton.setTitle("点中", for: .highlighted)
        view.addSubview(toGreenButton)
        toGreenButton.addTarget(self, action: #selector(changeToGreen), for: .touchUpInside)

        let toPurpleButton = UIButton(frame: CGRect(x: 100, y: 154, width: 44, height: 44))
        toPurpleButton.backgroundColor = .black
        toPurpleButton.setTitle("点中", for: .highlighted)
        view.addSubview(toPurpleButton)
        toPurpleButton.addTarget(self, action: #selector(changeToPurple), for: .touchUpInside)
    }

    @objc func changeToGreen() {
        CATransaction.begin()
        colorLayer.backgroundColor = UIColor.green.cgColor
        CATransaction.setAnimationDuration(3)
        let during = CATransaction.animationDuration()
        print("during:\(during)")
        colorLayer.frame = CGRect(x: 0, y: 400, width: 77, height: 77)
        CATransaction.commit()
    }

    @objc func changeToPurple() {
        colorLayer.backgroundColor = UIColor.purple.cgColor
        colorLayer.frame = CGRect(x: 200, y: 400, width: 77, height: 77)
    }
}

UIView 的隐式动画

通过代码验证 UIView 好像对隐式动画做了关闭操作。

  • 隐式动画实现过程
  1. 图层首先检测它是否有委托,并且是否实现CALayerDelegate协议指定的-actionForLayer:forKey方法。如果有,直接调用并返回结果。
  2. 如果没有委托,或者委托没有实现-actionForLayer:forKey方法,图层接着检查包含属性名称对应行为映射的actions字典。
  3. 如果actions字典没有包含对应的属性,那么图层接着在它的style字典接着搜索属性名。
  4. 最后,如果在style里面也找不到对应的行为,那么图层将会直接调用定义了每个属性的标准行为的-defaultActionForKey:方法。
class LayerView: UIView {
    override func action(for layer: CALayer, forKey event: String) -> CAAction? {
        let caAction = super.action(for: layer, forKey: event)
        switch caAction {
        case .some(let action):
            print(action) // <null>
        case .none:
            print("nil")
        }
        return caAction
    }
}

class ViewController: UIViewController {

    private let layerView = LayerView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        layerView.frame = CGRect(x: 0, y: 400, width: 77, height: 77)
        view.addSubview(layerView)
        let toGreenButton = UIButton(frame: CGRect(x: 100, y: 100, width: 44, height: 44))
        toGreenButton.backgroundColor = .blue
        toGreenButton.setTitle("点中", for: .highlighted)
        view.addSubview(toGreenButton)
        toGreenButton.addTarget(self, action: #selector(changeToGreen), for: .touchUpInside)

        let toPurpleButton = UIButton(frame: CGRect(x: 100, y: 154, width: 44, height: 44))
        toPurpleButton.backgroundColor = .black
        toPurpleButton.setTitle("点中", for: .highlighted)
        view.addSubview(toPurpleButton)
        toPurpleButton.addTarget(self, action: #selector(changeToPurple), for: .touchUpInside)
    }

    @objc func changeToGreen() {
        CATransaction.begin()
        layerView.backgroundColor = UIColor.green
        CATransaction.setAnimationDuration(3)
        let during = CATransaction.animationDuration()
        print("during:\(during)")
        layerView.frame = CGRect(x: 0, y: 400, width: 77, height: 77)
        CATransaction.commit()
    }

    @objc func changeToPurple() {
        layerView.backgroundColor = UIColor.purple
        layerView.frame = CGRect(x: 200, y: 400, width: 77, height: 77)
    }
}

每个UIView 对它关联的层进行委托,并提供 func action(for layer: CALayer, forKey event: String) -> CAAction?方法。如果在动画块中实现是,委托返回非空<CABasicAnimation: 0x280ce4640>;如果不在动画快中实现时,委托返回 <null>; 这样就实现了

如何关闭 UIView 的隐式动画

  1. UIView 中方法func action(for layer: CALayer, forKey event: String) -> CAAction?返回 nil
  2. 方法如下,在动画块中对所有属性关闭动画
//        CATransaction.begin()
        layerView.backgroundColor = UIColor.green
        CATransaction.setDisableActions(true)
        let during = CATransaction.animationDuration()
        print("during:\(during)")
        layerView.frame = CGRect(x: 0, y: 400, width: 77, height: 77)
//        CATransaction.commit()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值