更改ViewController时,UIImageView不会旋转

时间:2018-01-22 04:05:15

标签: ios swift uiviewcontroller uiimageview rotation

我在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)

2 个答案:

答案 0 :(得分:1)

当您从第二个导航控制器交易到导航堆栈上的第一个视图控制器时;第二个将不再被实例化,因为它仍然存在于内存中;那么viewDidLoad代表将不再被召唤;

如果您使用导航控制器,则可以弹出第二个,例如

self.navigationController?.popViewControllerAnimated(true)

或者

您可以覆盖viewWillAppear方法来调用您的功能

logo.startRotating(duration: 6)

答案 1 :(得分:1)

Kerim Sener你的旋转代码没有任何问题,只需在视图中调用函数,在视图控制器中显示如下方法..

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