自定义modal一个控制器的效果, presentViewController
presentViewController
一、主要用途
弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等。弹出模态ViewController主要使用于一下这几种情形:
1、收集用户输入信息
2、临时呈现一些内容
3、临时改变工作模式
4、相应设备方向变化(用于针对不同方向分别是想两个ViewController的情况)
5、显示一个新的view层级
这几种情形都会暂时中断程序正常的执行流程,主要作用是收集或者显示一些信息。
二、几个概念和常用设置
1、presenting view controller Vs presented view controller
当我们在view controller A中模态显示view controller B的时候,A就充当presenting view controller(弹出VC),而B就是presented view controller(被弹出VC)。官方文档建议这两者之间通过delegate实现交互,如果使用过UIImagePickerController从系统相册选取照片或者拍照,我们可以发现imagePickerController和弹出它的VC之间就是通过UIImagePickerControllerDelegate实现交互的。因此我们在实际应用用,最好也遵守这个原则,在被弹出的VC中定义delegate,然后在弹出VC中实现该代理,这样就可以比较方便的实现两者之间的交互。
2、Modal Presentation Styles(弹出风格)
通过设置presenting VC的modalPresentationStyle属性,我们可以设置弹出View Controller时的风格,有以下四种风格,其定义如下:
typedef enum { UIModalPresentationFullScreen = 0, UIModalPresentationPageSheet, UIModalPresentationFormSheet, UIModalPresentationCurrentContext,} UIModalPresentationStyle;
UIModalPresentationFullScreen代表弹出VC时,presented VC充满全屏,如果弹出VC的wantsFullScreenLayout设置为YES的,则会填充到状态栏下边,否则不会填充到状态栏之下。
UIModalPresentationPageSheet代表弹出是弹出VC时,presented VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击,这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域。
UIModalPresentationFormSheet这种模式下,presented VC的高度和宽度均会小于屏幕尺寸,presented VC居中显示,四周留下变暗区域。
UIModalPresentationCurrentContext这种模式下,presented VC的弹出方式和presenting VC的父VC的方式相同。
这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终已UIModalPresentationFullScreen模式显示presented VC。
3、Modal Transition Style(弹出时的动画风格)
通过设置设置presenting VC的modalTransitionStyle属性,我们可以设置弹出presented VC时场景切换动画的风格,其定义如下:
typedef enum { UIModalTransitionStyleCoverVertical = 0, UIModalTransitionStyleFlipHorizontal, UIModalTransitionStyleCrossDissolve, UIModalTransitionStylePartialCurl,} UIModalTransitionStyle;
我们可以看到有从底部滑入,水平翻转进入,交叉溶解以及翻页这四种风格可选。这四种风格在不受设备的限制,即不管是iPhone还是iPad都会根据我们指定的风格显示转场效果。
4、Dismiss Modal ViewController(消失弹出的VC)
消失presented VC,我们可以通过调用以下两个函数中的任何一个来完成
dismissModalViewControllerAnimated: // 将要废弃,不赞成继续使用dismissViewControllerAnimated:completion:
谁来调用这消失presented VC的这个方法:正确的做法是“谁污染谁治理”,即presenting VC调用上面的方法来取消presented VC的显示。这样做有一个好处,如果一个VC真不用户做的不同选择可能弹出不同的view controller,当不再需要显示被弹出的view controller的时候,直接调用[self dismissModalViewControllerAnimated]即可使之消失,而不用去关心其具体显示的哪一类view controller。当然系统在这里做了优化,当我们在presented VC里面调用上面的方法的时候,系统会自动的将这个消息传递到相应的presenting VC中,这样就可以实现不管谁弹出了自己,当不再需要的时候直接将自己消失掉的功能。在应用中具体要采用那种要看具体情况,如果presented VC需要和presenting VC有数据传递的话,建议在presenting VC实现的代理函数中dismiss弹出的view controller。
///////////////////////////////////// ios 8 ////////////////////////////////////////////////////////////////////////////////////////////////////
从iOS8开始,controller之间的跳转特效,需要用新的API UIPresentationController来实现。比如希望实现这样一个特效:显示一个模态窗口,大小和位置是自定义的,遮罩在原来的页面上。在iOS8之前,可以在viewWillAppear里设置superview的frame:
- - (void)presentModal:(NSDictionary*)result
- {
- YLSCheckoutSignatureController *controller = [[YLSCheckoutSignatureController alloc] initWithModel:result];
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
- controller.modalPresentationStyle = UIModalPresentationCustom;
- }else{
- controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
- controller.modalPresentationStyle = UIModalPresentationFormSheet;
- }
- [self presentViewController:controller animated:YES completion:nil];
- }
- -(void) viewWillAppear:(BOOL)animated
- {
- // in iOS8, handle by UIPresentationController
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
- return;
- }
- self.view.superview.layer.cornerRadius = 10;
- self.view.superview.layer.borderColor = [UIColor darkGrayColor].CGColor;
- self.view.superview.clipsToBounds = YES;
- self.view.superview.frame = CGRectMake(62, 114, 900, 540);
- }
但是以上的代码,在iOS8里就不再生效了,要用UIPresentationController来实现
首先明确一点,从Controller A->B,B的样式和跳转特效,还是由B来控制的。只不过以前是直接在Controller的生命周期方法里操作,而现在有专门的API来完成而已。这种设计也是合理的,否则如果从A可以跳转到B和C,但是样式和特效不一样,就只能通过在A里面设置实例变量来区分了,容易出错也很别扭。所以把跳转的行为由目标Controller来控制是很合理的
不过这组API的文档不太全,后续SDK升级可能会逐渐完善。以下介绍实现步骤:
目标Controller实现特定protocol
首先目标Controller要实现特定的协议,创建一个UIPresentationController
- @interface YLSCheckoutSignatureController : UIViewController<UIScrollViewDelegate, UIViewControllerTransitioningDelegate>
- self.transitioningDelegate = self;
- - (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
- {
- return [[YLSMainPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
- }
当条件满足时,iOS系统会调用这个方法,于是可以实例化自定义的UIPresentationController子类,定义跳转的样式和特效
自定义UIPresentationController
然后就要实现自定义的UIPresentationController,下面这段实例代码,实现居中展示一个自定义frame的模态页面,同时有半透明背景遮住原来的页面
- @implementation YLSMainPresentationController
- {
- UIView *dimmingView;
- }
- -(id) initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
- {
- self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
- if(self){
- dimmingView = [[UIView alloc] init];
- dimmingView.backgroundColor = [UIColor grayColor];
- dimmingView.alpha = 0.0;
- }
- return self;
- }
- - (void)presentationTransitionWillBegin
- {
- dimmingView.frame = self.containerView.bounds;
- [self.containerView addSubview:dimmingView];
- [self.containerView addSubview:self.presentedView];
- id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
- [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
- dimmingView.alpha = 0.5;
- } completion:nil];
- }
- - (void)presentationTransitionDidEnd:(BOOL)completed
- {
- if(!completed){
- [dimmingView removeFromSuperview];
- }
- }
- - (void)dismissalTransitionWillBegin
- {
- id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
- [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
- dimmingView.alpha = 0.0;
- } completion:nil];
- }
- - (void)dismissalTransitionDidEnd:(BOOL)completed
- {
- if(completed){
- [dimmingView removeFromSuperview];
- }
- }
- - (CGRect)frameOfPresentedViewInContainerView
- {
- return CGRectMake(62.f, 114.f, 900.f, 540.f);
- }
- @end
代码确实比以前复杂了一点,但是其实每个生命周期方法都是比较明确的,开发者可控的粒度也更细了。比如设置presented frame,就有专门的方法,只要返回CGRect就可以了,还是比较方便的
原始的ViewController发起跳转动作
经过前面2步,当自定义跳转发生时,就可以很细致地控制样式和跳转行为。接下来就是由原始controller(presenting view controller)来发起跳转动作:
- - (void)presentModal:(NSDictionary*)result
- {
- YLSCheckoutSignatureController *controller = [[YLSCheckoutSignatureController alloc] initWithModel:result];
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
- controller.modalPresentationStyle = UIModalPresentationCustom;
- }else{
- controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
- controller.modalPresentationStyle = UIModalPresentationFormSheet;
- }
- [self presentViewController:controller animated:YES completion:nil];
- }
关键是设置modalPresentationStyle为UIModalPresentationCustom,然后当presentViewController方法调用时,iOS系统就会创建出UIPresentationController的实例,来控制跳转的行为
自定义modal一个控制器的效果, presentViewController的更多相关文章
- 【Android初级】如何实现一个有动画效果的自定义下拉菜单
我们在购物APP里面设置收货地址时,都会有让我们选择省份及城市的下拉菜单项.今天我将使用Android原生的 Spinner 控件来实现一个自定义的下拉菜单功能,并配上一个透明渐变动画效果. 要实现的 ...
- 使用自定义 URL 实现控制器之间的跳转-b
一个app往往有很多界面,而界面之间的跳转也就是对应控制器的跳转,控制器的跳转一般有两种情况 push 或者 modal,push 和 modal 的默认效果是系统提供的 文章配图 1. 概述 系统提 ...
- 自定义view实现水波纹效果
水波纹效果: 1.标准正余弦水波纹: 2.非标准圆形液柱水波纹: 虽说都是水波纹,但两者在实现上差异是比较大的,一个通过正余弦函数模拟水波纹效果,另外一个会运用到图像的混合模式(PorterDuffX ...
- iOS基础 - Modal制作控制器
1.modal 1.modal推出控制器的代码 2.modal关闭当前控制器的代码 3.modal推出的动画效果 4.modal在ipad中应用 2.如何给控制器加上导航栏 3.modal和导航控制器 ...
- 日常学习随笔-自定义了一个MyArrayListDefin集合(数组扩容+迭代器+JDK1.8新方法+详细说明)
一.自定义了一个ArrayList的模拟集合(源码+详细说明) 前段时间分析了下ArrayList集合的源码,总觉得如果不自己定义一个的话,好像缺了点什么,所以有了如下的代码. 代码可以说是逐行注释了 ...
- 小程序自定义modal弹窗封装实现
前言小程序官方提供了 wx.showModal 方法,但样式比较固定,不能满足多元化需求,自定义势在必行~ 老规矩先上图 点击某个按钮,弹出 modal框,里面的内容可以自定义,可以是简单的文字提示, ...
- Python之自定义封装一个简单的Log类
参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...
- 创建自定义视图在Android矩阵效果画布教程
介绍 下面是一个快速教程,教你如何在Android中创建自定义视图.自定义视图创建一个矩阵雨效果. 本教程发布在http://www.androidlearner.com/. 背景 下面是关于如何工作 ...
- springmvc2 一个控制器写多个方法(非注解方式)
出处:http://blog.csdn.net/xuewenke/article/details/23895999 springmvc2 一个控制器写多个方法(非注解方式) 分类: spring 20 ...
随机推荐
- Redis学习手册(实例代码)
在之前的博客中已经非常详细的介绍了Redis的各种操作命令.运行机制和服务器初始化参数配置.本篇博客是该系列博客中的最后一篇,在这里将给出基于Redis客户端组件访问并操作Redis服务器的代码示例. ...
- Winform之跨线程访问控件(在进度条上显示字体)
此文章对于遇到必须使用线程但是没有办法在线程内操作控件的问题的处理 有很好的解决方案(个人认为的.有更好的方案欢迎交流.) 在做跨线程访问之前我们先了解下我们所做的需要达到的效果: 这个是批量的将x ...
- How to add route for IPV6 interface
Firewall : ifconfig eth1 inet6 add 2000::1/64 ifconfig eth4 inet6 add 5000::1/64 ------------------- ...
- [置顶] html学习笔记,锚点,超链接,table布局,表头,h,sub,blockquote,ul,li,ol.dl,加入收藏,打印,弹出窗口
<a name="shouye"></a> <strong>strong加粗</strong> <br> 没有加粗 &l ...
- 编译LFS
成功编译并运行linux from scratch 7.7 system,有必要作下总结.本次用的编译LFS的环境是: 虚拟机是virtalbox宿主系统为CentOS 7.0 x86_64 (cor ...
- Maven笔记(一)
1. 坐标 Maven坐标为各种构件引入了秩序,任何一个构件都有必须明确定义自己的坐标,而一组Maven坐标是通过一些元素定义的,它们是groupId, artifactId, version, pa ...
- 面试相关的技术问题---java基础
最近在准备秋季校招,将一些常见的技术问题做一个总结!希望对大家有所帮助! 1.面向对象和面向过程的区别是什么? 面向对象是把构成问题的事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描 ...
- PeekMessage
PeekMessage是一个Windows API函数.该函数为一个消息检查线程消息队列,并将该消息(如果存在)放于指定的结构. 1 语法 BOOL PeekMessage( LPMSG IpMsg, ...
- JS实现rgb与16进制颜色相互转换
1.rgb转16进制 function to16 (a) {//RGB(204,204,024) //十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a- ...
- 脱离Xcode,程序在模拟器中无法运行
今天在调试项目的时候 突然发现,如果项目不通过Xcode启动而是直接通过模拟器进行启动,程序闪一下马上退出,并且不是闪退,而是跑到后台去了,并且后台的程序同样无法启动.找了好多解决办法,最后的解决方案 ...