iosswiftuiviewanimationuiviewanimationtransition

UIView Animation completed instantly


So I've been working on a personal project, where I wanted to apply everything I have learned, but I have encountered this problem.

I have simplified this problem into this easy app.

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

}

However, when I change the view using the button mentioned above, I have set it to do animation under when it loads, but when it does, it is automatically in the final stage of the animation

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

Any advice will be appreciated. :)


Solution

  • if moving the animation to viewWillAppear or viewDidAppear like @imike suggested doesn't work, try using transition on the "loadingImageView" instead "animate"

     UIView.transition(with: self.loadingImageView,
                         duration: 10,
                         options: [. curveEaseInOut],
                         animations: {
    
                           self.loadingImageView.transform = CGAffineTransform(translationX: 0, y: 400)
       },
                         completion: nil)