美文网首页
隐式动画

隐式动画

作者: 流光还铅华 | 来源:发表于2021-11-17 19:08 被阅读0次
1、隐式动画是Core Animaiton的默认行为,当改变图层的动画属性时,能从先前值平滑过渡到新的值,而不是立即显示(默认过渡时间为0.25秒)

Core Animation基于一个假设,屏幕上的任何东西都可以(或者可能)做动画。 动画并不需要你在Core Animation中手动打开,相反需要明确地关闭,否则他会一 直存在。
当你改变 CALayer 的一个可做动画的属性,它并不能立刻在屏幕上体现出来。相反,它是从先前的值平滑过渡到新的值。这一切都是默认的行为,你不需要做额外 的操作。

2、CATransaction(事务管理类)

当你改变一个属性,Core Animation是如何判断动画类型和持续时间的呢?实际上动画执行的时间取决于当前事务的设置,动画类型取决于图层行为。

  • +begin
  • +commit
  • +setAnimationDuration:(设置当前事务的动画时间)
  • +animationDuration (获取值默认0.25s)

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

3、UIView自带动画方法
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion API_AVAILABLE(ios(4.0));

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion API_AVAILABLE(ios(4.0)); // delay = 0.0, options = 0

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations API_AVAILABLE(ios(4.0)); // delay = 0.0, options = 0, completion = NULL

/* Performs `animations` using a timing curve described by the motion of a spring. When `dampingRatio` is 1, the animation will smoothly decelerate to its final model values without oscillating. Damping ratios less than 1 will oscillate more and more before coming to a complete stop. You can use the initial spring velocity to specify how fast the object at the end of the simulated spring was moving before it was attached. It's a unit coordinate system, where 1 is defined as traveling the total animation distance in a second. So if you're changing an object's position by 200pt in this animation, and you want the animation to behave as if the object was moving at 100pt/s before the animation started, you'd pass 0.5. You'll typically want to pass 0 for the velocity. */
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion API_AVAILABLE(ios(7.0));

在iOS4中,苹果对UIView添加了一种基于block的动画方
法: +animateWithDuration:animations: 。这样写对做一堆的属性动画在语法上会更加简单,但实质上它们都是在做同样的事情。
CATransaction的+begin 和+commit方法
在内部自动调用,这样block中所有属性 的改变都会被事务所包含。这样也可以避免开发者由于对 +begin 和 +commit 匹 配的失误造成的风险。

4、CALayer隐式动画的实现

Core Animation通常对 CALayer 的所有属性(可动画的属性)做动画, 但是 UIView 把它关联的图层的这个特性关闭了 。为了更好说明这一点,我们需要 知道隐式动画是如何实现的。

当CALayer的属性被修改时,它会调用-actionForKey:方法,传递属性的名称,步骤如下:

  • 图层首先检测它是否有委托,并且是否实现 CALayerDelegate 协议指定的 - actionForLayer:forKey 方法。如果有,直接调用并返回结果。
  • 如果没有委托,或者委托没有实现- actionForLayer:forKey方法,图层接着检查包含属性名称对应行为映射的actions字典。
  • 如果 actions字典 没有包含对应的属性,那么图层接着在它的 style 字典接 着搜索属性名。
  • 最后,如果在 style 里面也找不到对应的行为,那么图层将会直接调用定义 了每个属性的标准行为的 -defaultActionForKey: 方法。
5、总结
  • UIView关联的图层禁用了隐式动画,对这种图层做动画的唯一办法就是使用 UIView的动画函数(而不是依赖CATransaction),或者继承UIView,并覆盖-actionForLayer:forKey:方法,或者直接创建一个显示动画

  • 对于单独存在的图层,我们可以通过实现图层的-actionForLayer:forKey:委托方法,或者提供一个actions字典来控制隐式动画

  • 隐式动画禁用方式
    1、[CATransaction setDisableActions:YES]
    2、重写- actionForLayer:forKey返回nil

相关文章

  • 取消(关闭)隐式动画

    取消(关闭)隐式动画** 可以通过动画事务(CATransaction)关闭默认的隐式动画效果[CATransac...

  • 隐式动画&显式动画 学习笔记

    隐式动画 CALayer的属性基本上都可以进行隐式动画 CATransaction可以控制隐式动画(执行时间或者关...

  • iOS-CALayer (四)

    上一篇 : iOS-CALayer (三) 前言:继续深入学习动画,主要从隐式动画、显式动画上车。 一、隐式动画 ...

  • iOS动画笔记

    在iOS各类动画效果中,习惯分为两类:隐式动画和显式动画。 隐式动画 简单的讲,由系统进行相关动画配置,执行动画效...

  • Core Animation小记(三)

    动画 1.隐式动画。没有制定任何动画的类型叫做隐式动画。事务,是通过CATransaction类来做管理,只能通过...

  • iOS之动画

    1.隐式动画 1.1.什么是隐式动画? 了解什么是隐式动画之前,要先了解是什么根层和非根层.根层:UIView内部...

  • iOS隐式动画与显式动画的区别

    请参考iOS隐式动画与显式动画的区别

  • SwiftUI -- View 动画

    SwiftUI 中的动画有两种类型:显式动画和隐式动画。 一、显式动画 显式动画通过 withAnimation ...

  • CoreAnimation

    隐式动画 所谓的隐式动画,之所以叫隐式是因为我们并没有指定任何动画的类型。我们仅仅改变了一个属性,然后CoreAn...

  • iOS动画-CAAnimation使用详解

    理解了隐式动画后,显式动画就更加通俗易懂了。区别于隐式动画的特点,显式动画就是需要我们明确指定类型、时间等参数来实...

网友评论

      本文标题:隐式动画

      本文链接:https://www.haomeiwen.com/subject/wcaltrtx.html