首页 > 解决方案 > UIView 动画瞬间完成

问题描述

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

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

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")
           }
}
}

任何建议将被认真考虑。:)

标签: iosswiftuiviewanimationuiviewanimationtransition

解决方案


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

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

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

推荐阅读