Beginning iOS Animations · Conclusion | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/9051-beginning-ios-animations/lessons/28

I am getting the following error in PopAnimator:
Cast from ‘UIView?’ to unrelated type ‘ViewController’ always fails

with this code in bold from the exercise:
//3 - Complete transition
if !self.presenting {
let viewController = transitionContext.view(forKey: .to) as! ViewController
viewController.selectedImage?.isHidden = false
}

@rramirez The above error occurs because you use view(forKey:) instead of viewController(forKey:) inside the if statement, so you try to cast a UIView to a UIViewController in this case.

To fix it, you should replace the following line of code:

let viewController = transitionContext.view(forKey: .to)  as!  ViewController

with this one:

let viewController = transitionContext.viewController(forKey: .to)  as!  ViewController

Please let me know if you have any other questions or issues about the whole thing.

Thank you!

Thank, You. That resolved my issue.