简体   繁体   English

CALayer Rotation动画在显示的ViewController上不起作用

[英]CALayer Rotation animation is not working on presented ViewController

I have two view controllers (ie firstVC and secondVC). 我有两个视图控制器(即firstVC和secondVC)。 I am presenting secondVC over firstVC and trying to achieve rotation animation but it's not working, I have tried running animation on the main thread with some delay but it's giving weird behavior. 我正在介绍firstVC而不是firstVC并尝试实现旋转动画,但是它不起作用,我尝试在主线程上运行动画有些延迟,但是它给出了奇怪的行为。 Following is the code I am using to rotate the view. 以下是我用来旋转视图的代码。

extension UIView {

private static let kRotationAnimationKey = "rotationanimationkey"

func rotate(duration: Double = 1, delay: Int = 0) {
    if layer.animation(forKey: UIView.kRotationAnimationKey) == nil {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")

        rotationAnimation.fromValue = 0.0
        rotationAnimation.toValue = Float.pi * 2.0
        rotationAnimation.duration = duration
        rotationAnimation.repeatCount = Float.infinity

        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(delay)) {
            self.layer.add(rotationAnimation, forKey: UIView.kRotationAnimationKey)
        }
    }
}

func stopRotating() {
    if layer.animation(forKey: UIView.kRotationAnimationKey) != nil {
        layer.removeAnimation(forKey: UIView.kRotationAnimationKey)
    }
}}

Note: By pushing 'secondVC' animation is working properly. 注意:按下'secondVC'动画即可正常工作。 But I wanted to present 'secondVC'. 但是我想介绍“ secondVC”。

I am not getting what's happening during the presentation of the view controller and rotation animation. 在演示视图控制器和旋转动画的过程中,我没有得到什么。

Please let me know if you find any solution. 如果您找到任何解决方案,请告诉我。

For using custom presentation transition you have to confirm to UIViewControllerTransitioningDelegate . 要使用自定义表示转换,您必须确认UIViewControllerTransitioningDelegate UIKit always call animationController(forPresented:presenting:source:) to see if a UIViewControllerAnimatedTransitioning is returned. UIKit总是调用animationController(forPresented:presenting:source:)来查看是否返回了UIViewControllerAnimatedTransitioning。 If it is returning nil then default animation is used. 如果返回nil,则使用默认动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM