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