简体   繁体   中英

How do I track the location of a UIView after rotation from a UIViewControllerTransitioningDelegate?

I'm working on a view controller transition (using a dedicated object conforming to UIViewControllerTransitioningDelegate ) where the view being presented zooms out from a subview in the presenting view. When the new view is dismissed it shrinks back into that same subview. Not too hard – but I'm stumped as to what I should do when the user rotates their device (or worse, if the window gets resized because of the new iPad multitasking) while on the presented view. How do I calculate the CGRect of the subview I want to shrink back in to? I'm thinking that I probably want to keep a reference to the subview in my transition object but how helpful is that when the subview is itself in a subview? (And without the whole view hierarchy I can't calculate with convertRect:FromView: ?)

For reference the Apple Photos app in iOS9 does exactly what I'm trying to accomplish. Tap a photo from a thumbnail to see it scale up to full-screen. Rotate your device and then hit the back button and the full-screen photo shrinks back to the correct location where the thumbnail should be in the rotated view.

Make a protocol with a method for returning the CGRect :

@protocol ZoomingTransitionFromViewDelegate <NSObject>
-(CGRect)rectForZoomingTransition;
@end

Make the presenting view controller adopt this protocol and pass the presenting view controller to the UIViewControllerTransitioningDelegate during initialization. It's then the job of the presenting view controller to calculate the rect for the view.

During dismissal make sure to set the frame of your toViewController to the frame of the containerView before calling for the rectForZoomingTransition so that the view can resize itself for any rotation or window changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM