SpringWithDamping for CALayer animations?

后端 未结 4 678
独厮守ぢ
独厮守ぢ 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:26

    I wrote a class to create CASpringAnimation instance. It works in a pretty simple way:

    By creating a spring animation from UIKit API, it arrests the created CASpringAnimation instance from the view's layer, copies it and returns it.

    But I don't know if it is App Store safe that to create CASpringAnimation in this way.

    import UIKit
    
    private let SharedCASpringAnimationFactory = CASpringAnimationFactory()
    
    public class CASpringAnimationFactory {
        private var dummyView: UIView
    
        private init() {
            dummyView = UIView(frame: CGRect.zeroRect)
        }
    
        private class var shared: CASpringAnimationFactory {
            return SharedCASpringAnimationFactory
        }
    
    
         public class func animation(#keyPath: String, dumping: CGFloat, initialSpringVelocity: CGFloat) -> CABasicAnimation {
            let duration = CATransaction.animationDuration()
    
            UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: dumping, initialSpringVelocity: initialSpringVelocity, options: nil,
                animations: { () -> Void in
                    CASpringAnimationFactory.shared.dummyView.bounds = CGRect(origin: CGPoint.zero, size: CGSize(width: 100, height: 100))
                }, completion: nil)
    
            let dummyLayer = CASpringAnimationFactory.shared.dummyView.layer
    
            let animations = dummyLayer.animationKeys().map {dummyLayer.animationForKey($0 as String) as CAAnimation}
    
            let arrestedAnimation = animations.first!.copy() as CABasicAnimation
            arrestedAnimation.keyPath = keyPath
            arrestedAnimation.fromValue = nil
            arrestedAnimation.toValue = nil
    
            dummyLayer.removeAllAnimations()
            shared.dummyView.bounds = CGRect.zeroRect
    
            return arrestedAnimation
        }
    }
    

提交回复
热议问题