Search code examples
uiimageviewios11uianimationswift4xcode9

Fade in/out Image in UIIamgeView happens with no duration


I have a UIIamge I created programmatically in viewDidLoad of a DetailViewController. I would like to .transitionCrossDissolve between its current image to another image. My problem is that once the DetailViewCOntroller is displayed the switch happens right away no matter the duration. I never even see the first image. I am using the following code.

    let rect = CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: view.frame.size.height / 3.0)
    let imageView = UIImageView(frame: rect)
    imageView.image = #imageLiteral(resourceName: "head1")
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    view.addSubview(imageView)
    UIView.transition(with: imageView,
                      duration: 16.0,
                      options: .transitionCrossDissolve,
                      animations: { imageView.image = #imageLiteral(resourceName: "head3") },
                      completion: nil)

Solution

  • Try to start the animation in the viewDidAppear delegate. Think thats the problem.