UIView动画瞬间完成

UIView Animation completed instantly

所以我一直在做一个个人项目,我想应用我所学的一切,但是我遇到了这个问题。

我已将这个问题简化为这个简单的应用程序。

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func button(_ sender: UIButton) {
    let nextVC = storyboard?.instantiateViewController(withIdentifier: "nextVC") as! SecondViewController
    nextVC.modalPresentationStyle = .fullScreen
    present(nextVC, animated: true)
}

}

但是,当我使用上面提到的按钮更改视图时,我已将其设置为在加载时执行动画,但加载时自动处于动画的最后阶段

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var loadingImageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    UIView.animate(withDuration: 10, animations: {
                   self.loadingImageView.transform = CGAffineTransform(translationX: 0, y: 400)
               }) { (_) in
                   print("Animation has been done")
           }
}
}

如有任何建议,我们将不胜感激。 :)

如果像@imike 建议的那样将动画移动到 viewWillAppear 或 viewDidAppear 不起作用,请尝试在 "loadingImageView" 上使用过渡而不是 "animate"

 UIView.transition(with: self.loadingImageView,
                     duration: 10,
                     options: [. curveEaseInOut],
                     animations: {

                       self.loadingImageView.transform = CGAffineTransform(translationX: 0, y: 400)
   },
                     completion: nil)