我有两个视图控制器(即firstVC和secondVC(。我在介绍secondVC而不是firstVC,并试图实现旋转动画,但它不起作用,我曾尝试在主线程上运行动画,但有一些延迟,但它给出了奇怪的行为。以下是我用来旋转视图的代码。
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"。
我不知道在视图控制器和旋转动画的演示过程中发生了什么。
如果你找到任何解决方案,请告诉我。
要使用自定义演示转换,必须向UIViewControllerTransitioningDelegate
确认。UIKit
总是调用animationController(forPresented:presenting:source:)
来查看是否返回UIViewControllerAnimatedTransitioning。如果返回nil,则使用默认动画。