我有两个视图控制器(即 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”。
我没有得到在视图控制器和旋转动画演示期间发生的事情。
如果您找到任何解决方案,请告诉我。