iOS8的UIPresentationController
本文转载至 http://kyfxbl.iteye.com/blog/2147888
从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的实例,来控制跳转的行为
iOS8的UIPresentationController的更多相关文章
- iOS8新特性(2)——UIPopoverController和UIPresentationController
一.以往使用 UIPopoverController 都是只在iPad上使用 /** * UIPopoverController 只能用于iPad,上,iPhone上使用会崩溃 */ -(void)o ...
- iOS iOS8新特性--UIPopoverPresentationController
1.回顾UIPopoverController的使用,下面这份代码只能在ipad下运行 // 初始化控制器,SecondViewController类继承自UIViewController Secon ...
- IOS开发之IOS8.0最新UIAlertController
最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.比如全新的UIPrese ...
- IOS开发之IOS8.0最新UIAlertController 分类: ios技术 2015-01-20 14:24 144人阅读 评论(1) 收藏
最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.比如全新的UIPrese ...
- 【Swift】UIPresentationController的使用方法
UIPresentationController是ios8.0的新特性哦,使用需要注意 先上一个效果图 第一步: 连线选择segue类型为,present Modally 第二步:需要演示的控制器,自 ...
- iOS:自定义模态动画 --UIPresentationController
UIPresentationController :展示控制器,是iOS8的一个新特性,用来展示模态窗口的.它是所有模态控制器的管理者. 即: 1> 管理所有Modal出来的控制器 2> ...
- iOS8需要兼容的内容
本文转载至 http://blog.csdn.net/liuwuguigui/article/details/39494435 1.iPad上使用presentModalViewController ...
- iOS8沙盒路径的变化
iOS8中的的沙盒路径发生了变化 之前是这样的路径,通过NSHomedictionary()获取的家路径 /Users/wupeng/Library/Application Support/iPhon ...
- iOS8中定位服务的变化(CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示)
最近在写一个LBS的项目的时候,因为考虑到适配iOS8,就将项目迁移到Xcode6.0.1上,出现了不能正常获取定位服务权限的问题. self.manger = [[CLLocationManager ...
随机推荐
- 超爽的Windows终端Cmder
我们常常看到科研.军事.编程上用的计算机系统是"黑洞洞"的,没有桌面.相对我们现在使用的Windows系统,既方便又美观,那么他们怎么不用Windows一样的图形化界面呢? 告诉你 ...
- C语言第四题
今天就一道题 阅读printf代码的具体实现,要求在阅读过程中要做下列的事 1.至少列出十个c标准库的方法,并且说明他们方法的含义,以及参数的含义 2.至少列出2个c标准库的引入(或者是依赖),并且说 ...
- c语言第二题
在我们的业务中经常会遇到很多业务,字符串会有一系列的操作,请写出以下的方法 1.写一个函数,给定char *p,char q,判断char *p中是否包含char q这个字符,包含则返回这个字符的下标 ...
- C/C++ XMPP/Jabber 客户端类库对比/点评 (转)
原文转自 http://blog.csdn.net/educast/article/details/31359835 1.gloox Ans. 老牌库,推荐 gloox是一个稳定功能完整的XMPP客户 ...
- configure: error: off_t undefined; check your library configuration
configure: error: off_t undefined; check your library configuration 发生背景: 编译PHP时出现的提示,报错信息为: configu ...
- socket的accept: Invalid argument问题
void local_sdk_server::wait_remote_client_connect_and_comm() { /*服务器服务启动,等待客户端的链接的到来*/ //sockaddr_in ...
- ARC072/ABC059
AtCoder Regular Contest 072 / Beginner Contest 059 Announcement <br > 猛然发现今天有一场AC.....然后..显示手残 ...
- 用JSONObject解析和处理json数据
(一)jar包下载 所需jar包打包下载百度网盘地址:https://pan.baidu.com/s/1c27Uyre (二)常见场景及处理方法 1.解析简单的json字符串: 1 // 简单的jso ...
- Javao中使用Jackson反序列时,将LinkedHashMap转成对象的方法(将任何Object类型转成实体)
可能存在这样一种情况,Jackson已经满足了大部分的序列化和反序列化工作,但是对于复杂的泛型实体估计未必能如愿的正常反序列,而此时对于一些泛型里面的实体对象就会反序列化成LinkedHashMap类 ...
- dubbo开发者指南
开发者指南 参与 流程 任务 版本管理 源码构建 框架设计 整体设计 模块分包 依赖关系 调用链 暴露服务时序 引用服务时序 领域模型 基本原则 扩展点加载 扩展点配置 扩展点自动包装 扩展点自动装配 ...