How to synchronously animate a UIView and a CALayer

后端 未结 2 2218
走了就别回头了
走了就别回头了 2021-02-18 13:58

To illustrate this question, I gisted a very small Xcode project on Github (two classes, 11kb download). Have a look at the gist here or use git clone git@gist.githu

2条回答
  •  我寻月下人不归
    2021-02-18 14:14

    If you want David Rönnqvist's snippet in Swift 3.0, here you have it:

        CATransaction.begin()
        CATransaction.setAnimationDuration(1.0)
        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut))
    
        // Layer animation
        let myAnimation = CABasicAnimation(keyPath: "frame");
        myAnimation.toValue = NSValue(cgRect: myNewFrame)
        myAnimation.fromValue = NSValue(cgRect: myLayer.frame)
    
        myLayer.frame = myNewFrame
        myLayer.add(myAnimation, forKey: "someKeyForMyAnimation")
    
        // Outer animation
        let outerAnimation = CABasicAnimation(keyPath: "frame")
        outerAnimation.toValue = NSValue(cgRect: myNewOuterFrame)
        outerAnimation.fromValue = NSValue(cgRect: outerView.frame)
    
        outerView.layer.frame = myNewOuterFrame
        outerView.layer.add(outerAnimation, forKey: "someKeyForMyOuterAnimation")
    
        CATransaction.commit()
    

提交回复
热议问题