Search code examples
iosswiftuiimageviewcore-animation

Combining an animation of an UIImageView with a transition


I am doing a transition of an UIImageView's image and would also like to move the image view at the same time. The results so far is that the image transitions, but not while moving.

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        UIView.transition(with: imageView, duration: 2.0, options: .transitionCrossDissolve, animations: {
            self.imageView.image = UIImage(named: "otherImage")
        }, completion: nil)

        imageViewTopConstraint.constant = 200

        UIView.animate(withDuration: 2.0) {
            self.view.layoutIfNeeded()
        }
    }

}

Any ideas on how to solve this?


Solution

  • You can put your imageView inside a container, then animate the constraints of the container while doing the transition on the imageView.