Animating the presenting view in a UIPresentationC

2020-02-29 19:46发布

For some context, I recommend reading this:

Very relevant question: "From View Controller" disappears using UIViewControllerContextTransitioning Very relevant answer: https://stackoverflow.com/a/25901154/751268


I'm trying to implement a custom view controller transition that animates the new view controller to cover half the screen, while simultaneously shrinking the presenting view controller to 90% (centered in the window, underneath the presented view controller).

First, my problem was that viewFromKey: returned nil. To solve that, the answer mentioned:

If you want to animate the presenting view controllers's view you should consider using UIModalPresentationFullscreen style or continue using UIModalPresentationCustom and implement your own subclass of UIPresentationController with shouldRemovePresentersView returning YES.

I did that, and viewFromKey: doesn't return nil anymore, but now the presenting view controller disappears completely (which makes sense considering I explicitly say it should by implementing shouldRemovePresentersView).

I add the presenting view controller's view to the container view, but it still gets removed. Is there anything else I should be doing to get this working?

Here's some relevant code:

UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];

BOOL show = self.isPresentation;
UIView *menuView = show ? toView : fromView;
UIView *backView = show ? fromView : toView;

UIView *containerView = [transitionContext containerView];

[containerView addSubview:backView];
[containerView addSubview:dimmedView];
[containerView addSubview:menuView];

// Adjust transforms, alpha and perform animations

I thought that by returning YES from shouldRemovePresentersView and manually adding it to the containerView, that should fix the issue, but backView gets removed anyway...

2条回答
看我几分像从前
2楼-- · 2020-02-29 20:03

I'm adding another answer, as my response is too long to fit in a comment.

First of all the viewForKey is available in iOS8, so unless you are targeting iOS8 only (why?) you should not use it, or use it after checking that the UIViewControllerContextTransitioning responds to that selector and use the viewControllerForKey for iOS7.

With that being said, it seems to me that this is a bug and I explain my self:

If you look at the UIPresentationController header file, you will see that it says

// Indicate whether the view controller's view we are transitioning from will be removed from the window in the end of the
// presentation transition
// (Default: YES)
- (BOOL)shouldRemovePresentersView;

So as you see the default is YES, so this should only overriden when you specifically want to say NO. However, you are right, without this being set explicitly to YES, the viewForKey for the UITransitionContextFromViewControllerKey remains nil.

I think you should fill in a bug report for this and for now use the viewControllerForKey which is fine to be used (nothing wrong with this) as it's not deprecated and works in both OS versions without a problem.

And the reason this is most likely a bug is that the viewForKey should return a view for theUITransitionContextFromViewControllerKey when the shouldRemovePresentersView is explicitly set to NO and not YES.

My 2 cents

查看更多
放我归山
3楼-- · 2020-02-29 20:11

Why are you setting shouldRemovePresentersView to YES if what you want is that the fromView remains visible?

I had the same problem as you, as in my custom 3d presentation the parent view controller was removed as soon as the transition was completed.

The solution there is to change the modalPresentationStyle to UIModalPresentationOverCurrentContext This will specifically prohibit the system to remove the owner viewController when the transition ends.

Still my 3D transition suffered from animation issues, when using this approach.

Si I ended up using a UIModalPresentationCustom modalPresentationStyle which solved almost all my issues, with the exception that the new view controller (which was a UINavigationController) would not move down when the in-call status bar would appear.

To solve this, I ended up changing the modalPresentationStyle back to UIModalPresentationFullScreen after the transition had been completed.

Here is my code that shows the new viewController with the custom presentation:

//show Login Screen
LoginViewController *viewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

UINavigationController *loginNavController = [[UINavigationController alloc] initWithRootViewController:viewController];
loginNavController.interactivePopGestureRecognizer.enabled = YES;
loginNavController.transitioningDelegate = self.customTransitionDelegate;
loginNavController.modalPresentationStyle = UIModalPresentationCustom;

[navigationController presentViewController:loginNavController animated:YES
                                 completion:^{
                                     //important, else status bar is not moving entire screen down....
                                     loginNavController.modalPresentationStyle = UIModalPresentationFullScreen;
                                 }
 ];

Also very important is that you must NOT add the old view to the containerView when you run your dismissal animation

So if the animation is the presenting, add your toView to the containerView

    UIView* inView = [transitionContext containerView];

    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
.....
......

    [inView insertSubview:toViewController.view aboveSubview:fromViewController.view];

But on the dismissal presentation DO NOT ADD the view to the containerView as it is still showing (since we specifically asked the system NOT to remove it), but simply animate between the two views.

查看更多
登录 后发表回答