在 UIPresentationController 中动画呈现视图 [英] Animating the presenting view in a UIPresentationController

查看:17
本文介绍了在 UIPresentationController 中动画呈现视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些上下文,我建议阅读此内容:

For some context, I recommend reading this:

非常相关的问题:来自视图控制器"使用 UIViewControllerContextTransitioning 消失非常相关的答案:https://stackoverflow.com/a/25901154/751268

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

我正在尝试实现一个自定义视图控制器过渡,该过渡动画使新的视图控制器覆盖屏幕的一半,同时将呈现的视图控制器缩小到 90%(以窗口为中心,呈现的视图控制器下方).

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).

首先,我的问题是 viewFromKey: 返回 nil.为了解决这个问题,答案提到:

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

如果您想为呈现视图控制器的视图设置动画,您应该考虑使用 UIModalPresentationFullscreen 样式或继续使用 UIModalPresentationCustom 并实现您自己的 UIPresentationController 子类> shouldRemovePresentersView 返回 YES.

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.

我这样做了,并且 viewFromKey: 不再返回 nil,但是现在呈现的视图控制器完全消失了(考虑到我明确说它应该通过实现 shouldRemovePresentersView).

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?

这是一些相关的代码:

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

我认为通过从 shouldRemovePresentersView 返回 YES 并手动将其添加到 containerView,应该可以解决问题,但是 backView 无论如何都会被删除...

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...

推荐答案

我正在添加另一个答案,因为我的回复太长,无法在评论中列出.

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

首先 viewForKey 在 iOS8 中可用,所以除非你只针对 iOS8(为什么?)你不应该使用它,或者在检查 UIViewControllerContextTransitioning 之后使用它code> 响应该选择器并为 iOS7 使用 viewControllerForKey.

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:

如果你查看 UIPresentationController 头文件,你会看到它说

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;

如您所见,默认值为 YES,因此仅当您特别想说 NO 时才应覆盖此设置.但是,您是对的,如果没有将其显式设置为 YES,UITransitionContextFromViewControllerKeyviewForKey 保持为零.

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.

我认为你应该为此填写一个错误报告,现在使用 viewControllerForKey 很好用(这没有错),因为它没有被弃用并且在两个操作系统版本中都没有一个问题.

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.

这很可能是一个错误的原因是 viewForKey 应该为 UITransitionContextFromViewControllerKey when shouldRemovePresentersView 返回一个视图 明确设置为 NO 而不是 YES.

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.

我的 2 美分

这篇关于在 UIPresentationController 中动画呈现视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆