UIPresentationController
UIPresentationController
UIPresentationController用于管理过渡动画和屏幕上控制器的presentation
从一个控制器被presented到dismissed,UIKit使用presentation控制器来管理视图控制器的显示过程的各个方面。presentation可以在animator对象提供的动画之上添加自己的动画,可以响应size的变化,并且可以管理视图控制器在屏幕上的呈现方式的其他方面
当使用present(_:animated:completion:)
方法present视图控制器时,UIKit始终管理展示过程。 该过程的一部分涉及创建适合给定表示样式的表示控制器。 对于内置样式(例如UIModalPresentationStyle.pageSheet
样式),UIKit定义并创建所需的presentation控制器对象。 你的应用程序唯一可以提供自定义表示控制器,是在设置视图控制器modalPresentationStyle
属性为UIModalPresentationStyle.custom
时。 当你想在要显示的视图控制器下方添加阴影视图或装饰视图时,或者想要以其他方式修改显示行为时,可以提供自定义的显示控制器。
可以通过控制器的transitioning委托,提供自定义的presentation
对象
UIViewControllerTransitioningDelegate有如下的方法:
func presentationController(forPresented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
表示的是:提供一个UIPresentationController
,用于管理视图层次结构
在Presentation中添加自定义View
- 当一个控制器即将被presented,UIKit调用presentation控制器的
presentationTransitionWillBegin()
方法。可以使用这个方法将view添加到view层级中,并设置相关的动画 - 在presentation结束的时候,UIKit调用
presentationTransitionDidEnd(_:)
方法 - 在dismissing控制器的时候,使用
dismissalTransitionWillBegin()
方法来配置动画,使用dismissalTransitionDidEnd(_:)
方法从视图层级中移除自定义view
如下的例子,添加了一个dimming view作为背景,在presentationTransitionWillBegin()
方法中添加dimming view,然后淡入动画和过渡动画一起执行。在presentationTransitionDidEnd(_:)
中移除dimming view。
- (void)presentationTransitionWillBegin {// Add a custom dimming view behind the presented view controller's view[[self containerView] addSubview:_dimmingView];[_dimmingView addSubview:[[self presentedViewController] view]];// Use the transition coordinator to set up the animations.id <UIViewControllerTransitionCoordinator> transitionCoordinator =[[self presentingViewController] transitionCoordinator];// Fade in the dimming view during the transition.[_dimmingView setAlpha:0.0];[transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {[_dimmingView setAlpha:1.0];} completion:nil];
}- (void)presentationTransitionDidEnd:(BOOL)completed {// Remove the dimming view if the presentation was aborted.if (!completed) {[_dimmingView removeFromSuperview];}
}
Adapting Size Class的改变
Presentation控制器管理size class的改变,通过调整presentation style。只是在新场景中,当前的 presentation style 没有意义时才调整。例如,当水平的size class从regular变为compact时,popover弹窗将变为full-screen全屏。
当当水平的size class从regular变为compact时,presentation控制器调用adaptivePresentationStyle
方法来确定应用那种新样式。因为presentation控制器对象可能会改变,所以应该从presented控制器的presentationController
属性获取presentation控制器。
当presentation style 改变时,presentation控制器可以让你有机会指定新的presented view controller。 在开始过渡之前,presentation控制器调用其委托对象的presentationController(_:viewControllerForAdaptivePresentationStyle:)
方法。
响应Size的改变
Size改变表示的是控制器view的高或者宽的微小变化。通常,这些变化会在设备在纵向和横向之间旋转时发生,当size发生变化时,UIKit会调用presentation控制的viewWillTransition(to:with:)
方法。
在通知你的演示presentation控制器即将发生大小更改后,UIKit会开始正常的视图布局过程。使用自动布局的应用程序无需执行任何操作,因为自动布局机制会根据需要调整视图的大小。但是,如果自定义presentation控制器需要进行特定于布局的更改,则可以在containerViewWillLayoutSubviews()
和 containerViewDidLayoutSubviews()
方法中进行更改。这些方法等价于UIViewController类的viewWillLayoutSubviews()
和viewDidLayoutSubviews()
方法。
继承
继承UIPresentationController
,在初始化的过程中需要调用init(presentedViewController:presenting:)
方法,这是这个类的指定初始化器
方法重写
如果你的UIPresentationController
子类管理一个或多个自定义视图,则应考虑重写以下方法:
presentationTransitionWillBegin()
添加自定义的view到view层级中。如果view controller 不能被展示,可以在presentationTransitionDidEnd(_:)
中移除dismissalTransitionWillBegin()
方法执行dismissal动画,在dismissalTransitionDidEnd(_:)
方法中移除自定义的view- 使用
viewWillTransition(to:with:)
方法执行size-related的改变
子类可以根据需要重写此类的其他方法以提供自定义行为。 例如,您可以覆盖shouldPresentInFullscreen
或frameOfPresentedViewInContainerView
方法,并返回与默认实现所提供的值不同的值。
例子
参考:
- UIPresentationController Tutorial: Getting Started
1.presented的控制器有个transitioning delegate 负责加载UIPresentationController
和 presentation 、dismissal动画。 该委托是一个符合UIViewControllerTransitioningDelegate
的对象。
2.UIPresentationController
的子类有许多自定义presentation的方法
3.animation 控制器对象,负责presentation和dismissal动画,遵循UIViewControllerAnimatedTransitioning
协议
4.presentation控制的代理,告知presentation控制器,当trait collection改变时,该怎么做。为了做到自适应,应该遵循UIAdaptivePresentationControllerDelegate
协议
例子重写的UIPresentationController
方法
1.重写presentationTransitionWillBegin()
方法,让dimming view淡入
UIPresentationController
有个containerView
属性,持有视图层级UIPresentationController
的transitionCoordinator
属性,有方法可以在transition是添加动画
//presentation动画override func presentationTransitionWillBegin() {guard let dimmingView = dimmingView else {return}containerView?.insertSubview(dimmingView, at: 0)NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[dimmingView]|",options: [],metrics: nil,views: ["dimmingView": dimmingView]))NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[dimmingView]|",options: [],metrics: nil,views: ["dimmingView": dimmingView]))guard let coordinator = presentedViewController.transitionCoordinator else {dimmingView.alpha = 1.0return}coordinator.animate(alongsideTransition: { _ inself.dimmingView.alpha = 1.0})}
2.重写presentationTransitionWillBegin()
方法,让dimming view淡出
override func dismissalTransitionWillBegin() {guard let coordinator = presentedViewController.transitionCoordinator else {dimmingView.alpha = 0.0return}coordinator.animate(alongsideTransition: { _ inself.dimmingView.alpha = 0.0})}
3.重写containerViewWillLayoutSubviews
方法,以响应presentation控制器的containerView
的布局的改变
override func containerViewWillLayoutSubviews() {presentedView?.frame = frameOfPresentedViewInContainerView
}
4.提供presented控制器内容的size给presentation控制器
override func size(forChildContentContainer container: UIContentContainer,withParentContainerSize parentSize: CGSize) -> CGSize {switch direction {case .left, .right:return CGSize(width: parentSize.width*(2.0/3.0), height: parentSize.height)case .bottom, .top:return CGSize(width: parentSize.width, height: parentSize.height*(2.0/3.0))}
}
5.重写frameOfPresentedViewInContainerView
,计算presented view的size,返回frame
//动画结束时presented view的frameoverride var frameOfPresentedViewInContainerView: CGRect {var frame: CGRect = .zeroframe.size = size(forChildContentContainer: presentedViewController,withParentContainerSize: containerView!.bounds.size)switch direction {case .right:frame.origin.x = containerView!.frame.width*(1.0/3.0)case .bottom:frame.origin.y = containerView!.frame.height*(1.0/3.0)default:frame.origin = .zero}return frame}
例子的效果如下:
UIPresentationController相关推荐
- iOS:自定义模态动画 --UIPresentationController
UIPresentationController :展示控制器,是iOS8的一个新特性,用来展示模态窗口的.它是所有模态控制器的管理者. 即: 1> 管理所有Modal出来的控制器 2> ...
- UIPresentationController 教程:入门
原文:UIPresentationController Tutorial: Getting Started 作者:Ron Kliffer 译者:kmyhy 很久以来,View Controller P ...
- 在iOS 8中使用UIAlertController
本文转载至 http://www.cocoachina.com/ios/20141126/10320.html iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨 ...
- (0030) iOS 开发之跳转之转场动画
demo: 1 iOS ~ ViewController的Push,Pop和Present,Dismiss转场动画 // http://blog.csdn.net/zhangao0086/artic ...
- popover带箭头弹框
我们先来看一下效果吧: 分析:这个带箭头的弹框其实是一个控制器,通过Modal方式展现,但跟传统模态方式效果不一样,我们一眼就能看出. Xib方式实现popover: 1.segue的时候选择Pres ...
- iOS自定义转场动画实战讲解
转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completio ...
- 01-Popover跳转
Popover跳转 1.1-简介 什么是Popover? UIViewController进行跳转的时候,可以设置跳转样式 Popover样式是iOS8加入的新特性 1.2-在StoryBoard中使 ...
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记24 popovers弹窗
上几话中我们详细了解了几种segue,我们也了解到了多MVC模式的几种控制器,比如导航.选项卡和分栏,除了这三种多MVC的模式之外,还有一种popover,它跟其他三种不太一样.首先先来认识一下pop ...
- iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)
1 #import "ViewController.h" 2 3 @interface ViewController ()<UIAlertViewDelegate,UIAct ...
最新文章
- LinuxMint 14 更新源(能成功的!)
- LinKedHashMap和TreeMap介绍
- matlab的index函数,写论文第九天:MATLAB之rsindex函数
- usb管控软件_记账软件新外设:PUPUPULA 智能存钱罐登陆众筹
- kubernetes目录挂载
- c语言字体取模软件下载,非常好用的lcd汉字取模软件下载_非常好用的lcd汉字取模软件官方下载-太平洋下载中心...
- MySQL学习(三)
- drools 7.x 事件元数据
- 库没启动,报:ORA-01034: ORACLE not available
- 数据结构之队列java版
- 回顾:饶毅教授的一次采访
- LPC1788 IAP的实现及遇见的问题
- python程序memory error_科学网—[转载]解决Python memory error的问题(四种解决方案) - 高琳琳的博文...
- dwf是什么格式文件
- 微信绑定银行卡消息服务器,微信绑定了银行卡,如果不注意这个消息,可能会出现自动扣费...
- Prometheus+SpringBoot应用监控全过程详解
- qq华夏服务器状态,最国产!QQ华夏199组服务器皆“国名”
- 通信原理课程设计报告
- 家用计算机中PCB板材质,介绍PCB电路板的主要原材料
- 如何理解假设检验中的假设设计?