ios - 在 UIPresentationController 中动画呈现 View

标签 ios animation uiviewcontroller

对于某些情况,我建议阅读以下内容:

非常相关的问题:"From View Controller" disappears using UIViewControllerContextTransitioning
非常相关的答案:https://stackoverflow.com/a/25901154/751268

我正在尝试实现一个自定义 View Controller 转换,该动画使新 View Controller 覆盖一半屏幕,同时将呈现 View Controller 缩小到 90%(在窗口中居中,在呈现 View Controller 下方)。

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

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不再,但是现在呈现的 View Controller 完全消失了(考虑到我明确地说应该通过实现 shouldRemovePresentersView ,这是有道理的)。

我将呈现 View Controller 的 View 添加到容器 View 中,但它仍然被删除。我还应该做些什么来使它正常工作吗?

这是一些相关的代码:
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

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

最佳答案

我正在添加另一个答案,因为我的回复太长,无法放入评论中。

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

话虽如此,在我看来这是一个错误,我解释了我自己:

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

// 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;

如您所见,默认值为"is",因此仅当您特别想说“否”时才应覆盖它。
但是,您是对的,如果没有将其明确设置为 YES,viewForKey对于UITransitionContextFromViewControllerKey仍然为零。

我认为您应该为此填写错误报告,现在使用 viewControllerForKey可以使用(这没什么问题),因为它没有被弃用并且在两个操作系统版本中都可以正常工作。

这很可能是一个错误的原因是 viewForKey应该返回 UITransitionContextFromViewControllerKey 的 View shouldRemovePresentersView显式设置为 而不是 .

我的 2 美分

关于ios - 在 UIPresentationController 中动画呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26851471/

相关文章:

ios - 是否每个具有 'Edit' 或 'Done' 按钮的 View 都​​需要位于 UINavigationController 内?

ios - iOS (iPhoneX) 上的 html5 视频播放

javascript - 淡入淡出滑动 Javascript

jquery - jQuery .toggle() 动画期间文本暂时下降

animation - 如何使用 UIViewKeyframeAnimationOptionRepeat 选项停止 [UIView animateKeyframesWithDuration] 的动画

ios - 自定义 iOS 7 交互式流行动画在动画后变为空白

ios - 将表格的刷新控制置于底部

ios - Swift 3 - 调用准备转场时 View 不会更新

ios - 如何在 SwiftUI 项目中使用 AVAudioPlayer 播放音频

ios - 在 iOS 13 的 iPhone 上将纵向 View Controller 推到横向 View Controller 上