SpringWithDamping for CALayer animations?

后端 未结 4 679
独厮守ぢ
独厮守ぢ 2021-02-05 04:00

After playing around a lot with the UIView dynamic animations introduced in iOS 7, most notably:

[UIView animateWithDuration: delay: usingSpringWi         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 04:10

    in iOS9 Apple finally made the CASpringAnimation class public.

    You can use it like that:

    let spring = CASpringAnimation(keyPath: "position.x")
    spring.damping = 5
    spring.fromValue = myLayer.position.x
    spring.toValue = myLayer.position.x + 100.0
    spring.duration = spring.settlingDuration
    myLayer.addAnimation(spring, forKey: nil)
    

    Notice that you cannot set the animation duration - you need to ask the CASpringAnimation class for the settlingDuration (e.g. "How much time is going to take for the spring system to settle down") and then set it as the duration of your animation.

    Check the header files for CASpringAnimation - it exposes a number of spring system variables you can adjust - stiffness, mass, etc.

提交回复
热议问题