美文网首页
iOS中的CASpringAnimation

iOS中的CASpringAnimation

作者: 黑猫呵呵 | 来源:发表于2020-01-20 18:56 被阅读0次

    iOS开发时,我们可能会遇到需要让控件实现弹框效果的动画。


    通常来说,UIKit框架已经为我们封装好了简单的使用方法

    ```

    class func animate(withDuration duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping dampingRatio: CGFloat, initialSpringVelocity velocity: CGFloat, options: UIView.AnimationOptions = [], animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil)

    ```

    不过若想要实现更真实,流畅,可靠的spring动画,我们还是需要借助CABaseAnimation的子类CASpringAnimation

    使用的方法也很简单,相对于CABaseAnimation, CASpringAnimation拥有自己的独有的5个关键属性,下面将详细介绍这5个属性。

    1. 阻尼系数

    ```

    var damping: CGFloat { get set }

    ```

    官方描述: 

    The default value of the damping property is 10. Reducing this value reduces the energy loss with each oscillation: the animated value will overshoot the toValue and the settlingDuration may be greater than the duration. Increasing the value increases the energy loss with each duration: there will be fewer and smaller oscillations and the settlingDuration may be smaller than the duration.

    默认值为10。增大该值,就会增大每次弹簧运动损失的能量,减小运动的幅度,所以弹簧运动会更快的结束。  

    我们可以简单的理解为——摩擦力。

    当damping为0时,没有了摩擦力,动画将永远不会停止。

    2.初始速度

    ```

    var initialVelocity: CGFloat { get set }

    ```

    官方描述: 

    Defaults to 0, which represents an unmoving object. Negative values represent the object moving away from the spring attachment point, positive values represent the object moving towards the spring attachment point.

    默认值为0。 即默认情况下没有初始速度。

    如蹦极运动,initialVelocity = 0 意为我们自由下落。

    而initialVelocity > 0, 可以意为我们站在蹦极点不愿下落,工作人员为了工作能正常进行,使劲把你扔了下去。可以想象出,此时我们下落的最低点儿肯定比我们自由落体是的要低,即当initialVelocity > 0时,我们的弹性运动会获得更大的运动幅度。

    相反的initialVelocity < 0,即意为我们会从相反的方向运动。

    3.mass 质量

    ```

    var mass: CGFloat { get set }

    ```

    官方解释

    The default mass is 1. Increasing this value will increase the spring effect: the attached object will be subject to more oscillations and greater overshoot, resulting in an increased settlingDuration. Decreasing the mass will reduce the spring effect: there will be fewer oscillations and a reduced overshoot, resulting in a decreased settlingDuration.

    质量默认为1。 

    若更大该属性,物体将会获得更大的惯性,从而导致运动的幅度更大,运动趋于稳定的时间增加。而减小改属性,即物体的惯性变小,从而导致运动幅度减小,运动趋于稳定的时间减少。

    4.stiffness 刚度

    ```

    var stiffness: CGFloat { get set }

    ```

    官方解释

    The default stiffness coefficient is 100. Increasing the stiffness reduces the number of oscillations and will reduce the settling duration. Decreasing the stiffness increases the the number of oscillations and will increase the settling duration.

    可以理解为弹簧的硬度。 刚度越大,弹性运动次数就会越少,趋于稳定的时间也会越少。

    5.settlingDuration 趋于稳定的时间

    ```

    var settlingDuration: CFTimeInterval { get }

    ```

    基于CASpringAnimation的动画运动的时间是由以上4个属性决定的,而这个决定的时间即为settlingDuration。  我们平常使用若是想要动画完整展示需要这样去设置,

    过大的duration会导致弹性运动已经停止,而动画还没有结束,

    过小的duration会导致弹性运动还没有结束,而动画已经结束。

    let anim = CASpringAnimation(keyPath: "transform.scale")

    anim.initialVelocity = ...

    anim.mass = ...

    anim.stiffness = ...

    anim.damping = ...

    anim.fromValue = ...

    anim.toValue = ...

    anim.duration = anim.settlingDuration

    ```

    好了,介绍就这么多了。赶快动手试试自己想用到的spring动画吧~

    相关文章

      网友评论

          本文标题:iOS中的CASpringAnimation

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