Group Group Group Group Group Group Group Group Group

iOS_Animations_by_Tutorials_v4.0.1 Chapter 17

#1

When we present the HerbDetailsViewController rotate to landscape orientation and dismiss the HerbDetailsViewController the viewController looks terrible. How can I fix this?

1 Like
#2

@carlos Could you please explain what happens exactly in this case? Thank you!

1 Like
#3

1 Like
#4

I encountered this issue as well. To see it, run the final version of the app, then click on an image to bring up the detail view. While the detail is showing, rotate the phone to change the orientation. Then click on the image to dismiss the detail. The main view is mangled.

After much floundering around, I discovered that when you set the transitioningDelegate, you should also set modalPresentationStyle to .custom (this is in didTapImageView):

herbDetails.transitioningDelegate = self
herbDetails.modalPresentationStyle = .custom

This will let the main ViewController do things right during a change in orientation, even when the detail view is showing. However, it does break a few things. For instance, in animateTransition, in PopAnimator, the toView will be nil when dismissing. So you have to do some tweaking (toView!).

Once you get that fixed, you will realize that setting OriginFrame when you display the detail view is not going to work well if the orientation changes while the detail view is displayed. The frame of the image in question will also change when the orientation changes.

To fix that, I changed it so that you get a weak reference to the selectedImage instead, and get the frame from it when you are animating the transition. Just in time, in other words.

//declared in PopAnimator

weak var originImage: UIImageView?

// set in the transitioning delegate in ViewController, both presenting and dismissing:

transition.originImage = selectedImage

//guard at start of animateTransition in PopAnimator:

guard let originImage = originImage else {
transitionContext.completeTransition(false)
return
}

//retrieve originFrame on the fly in animateTransition:

let originFrame = originImage.superview!.convert(originImage.frame, to: nil)

Then you can get back to the business of animating the cornerRadius, and fading the text in and out, and maybe updating the listView.contentOffset when the orientation changes.

1 Like
closed #5

This topic was automatically closed after 166 days. New replies are no longer allowed.