1

我有两个视图控制器(即 firstVC 和 secondVC)。我在 firstVC 上呈现 secondVC 并尝试实现旋转动画,但它不起作用,我尝试在主线程上运行动画,但有一些延迟,但它给出了奇怪的行为。以下是我用来旋转视图的代码。

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)
    }
}}

注意:通过推动“secondVC”动画正常工作。但我想介绍“secondVC”。

我没有得到在视图控制器和旋转动画演示期间发生的事情。

如果您找到任何解决方案,请告诉我。

4

1 回答 1

-1

要使用自定义演示转换,您必须确认UIViewControllerTransitioningDelegate. UIKit始终调用animationController(forPresented:presenting:source:)以查看是否返回 UIViewControllerAnimatedTransitioning。如果它返回 nil,则使用默认动画。

于 2018-07-20T07:09:01.440 回答