Yes you’re right, the animator crashes because it tried to unwrap a view which is not available anymore.
The property toView
which you used during the presentation actually becomes a fromView
during dismissal.
It was used earlier in the tutorial but is actually not needed at the end because we’re using recipeView
instead.
To fix it you can simply remove the property toView
and incorporate it into recipeView
like so:
let containerView = transitionContext.containerView
let recipeView = presenting ? transitionContext.view(forKey: .to)! : transitionContext.view(forKey: .from)!
Note how the faulty line has been removed in the middle an added in recipeView
Doing this will trigger another error a bit further in the method as it’s trying to add toView
as subview, which is not possible because we juste removed it. Lucky for us we incorporated it in recipeView
so you can add that one instead:
containerView.addSubview(recipeView)
containerView.bringSubviewToFront(recipeView)
Tadaa, the whole shebang should work again!
Enjoy your new animation!