ios - 当我更改 ViewController 时 UIImageView 不旋转

标签 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)

最佳答案

Kerim Sener 在您的旋转代码中没有任何问题,只是调用 View 中的函数确实出现了如下所示的方法......在您的 View Controller 中。

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

关于ios - 当我更改 ViewController 时 UIImageView 不旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48374521/

相关文章:

ios - Swift - 告诉 TableViewController 填充什么数据

ios - 在 Swift 中通过计数从多维数组中查找最大数组

iphone - 在 Windows 上编译和签署 xcode 项目

ios - 保留 UITableViewCell 子类属性

ios - 无法从另一个 TableView 加载另一个 TableView

ios - 根据所选的 View Controller 调用不同的方法

ios - 等到在 ios 中执行委托(delegate)方法

iphone - 如何获取单个 UIBarButtonItem 的多个事件

ios - 从 Application Support Directory 中的 plist 中读取翻译

ios - 嵌套的 CollectionView 不会在滑动时隐藏导航栏