当我更改 ViewController 时 UIImageView 不旋转

UIImageView does not rotate when I change the ViewController

我在 ViewController 中有一个旋转的 UIImageView。但是当我将 ViewController 更改为 SecondViewController 并将 SecondViewController 更改为 ViewController.

时,UIImageView 不会旋转

分机;

import Foundation
import UIKit

extension UIView {

    //Start Rotating view
    func startRotating(duration: Double = 1) {
        let kAnimationKey = "rotation"

        if self.layer.animation(forKey: kAnimationKey) == nil {
            let animate = CABasicAnimation(keyPath: "transform.rotation")
            animate.duration = duration
            animate.repeatCount = Float.infinity

            animate.fromValue = 0.0
            animate.toValue = Float(Double.pi * 2.0)
            self.layer.add(animate, forKey: kAnimationKey)
        }
    }

    //Stop rotating view
    func stopRotating() {
        let kAnimationKey = "rotation"

        if self.layer.animation(forKey: kAnimationKey) != nil {
            self.layer.removeAnimation(forKey: kAnimationKey)
        }
    }

}


viewdidload ;

logo.startRotating(duration: 6)

当您从第二个导航控制器向导航堆栈上的第一个视图控制器进行事务处理时;第二个不会被再次实例化,因为它仍然存在于内存中;那么 viewDidLoad 委托将不会被再次调用;

然后你可以弹出第二个,如果你使用的是导航控制器,例如

self.navigationController?.popViewControllerAnimated(true)

或者

您可以覆盖 viewWillAppear 方法来调用您的函数

logo.startRotating(duration: 6)

Kerim Sener 在你的旋转代码中没有任何问题只是调用视图中的函数确实出现了如下方法..在你的视图控制器中。

override func viewDidAppear(_ animated: Bool) {
        logo.startRotating(duration: 6)
    }