【Swift】UIPresentationController的使用方法
UIPresentationController是ios8.0的新特性哦,使用需要注意
先上一个效果图

第一步: 连线选择segue类型为,present Modally
第二步:需要演示的控制器,自定义为HJCPopViewController
第三步:新建一个类HJCPopoverPresentationController,继承UIPresentationController
第四步:在HJCPopoverPresentationController类中实现以下代码
// 遮罩视图
lazy var dummyView: UIView = { let v = UIView()
v.backgroundColor = UIColor(white: 0.0, alpha: 0.2) // 添加手势识别,可以隔离所有底层控件
let tap = UITapGestureRecognizer(target: self, action: "clickDummyView")
v.addGestureRecognizer(tap) return v
}() func clickDummyView() {
// 关闭被 Modal 的视图控制器
self.presentedViewController.dismissViewControllerAnimated(true, completion: nil)
} /**
presentedViewController: 要 modal 显示的视图控制器
presentingViewController: 文档说,底层的视图控制器,但是实测是nil containerView 容器视图,构造函数中,是 nil,符合懒加载的原则
presentedView() 被展现的视图
containerViewWillLayoutSubviews 将要布局子视图,自定义 presentedView 的大小和位置
*/
override func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews() // // 设置遮罩视图的大小
// dummyView.frame = containerView.bounds
// containerView.insertSubview(dummyView, atIndex: 0) // 设置目标视图的大小
presentedView().frame = CGRectMake(100, 56, 200, 240)
}
第五步:在主控制器中设置目标 跳转控制器相关属性
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let popVC = segue.destinationViewController as! HJCPopoverPresentationController
/**
要实现自定义Modal转场
1. 设置转场代理
返回 UIPresentationController,由 该控制器,负责 Modal 界面的处理
2. 指定Modal的展现方式是自定义
*/
// transition 转场,从一个界面跳到另外一个界面
popVC.transitioningDelegate = self
// 设置展现方式
popVC.modalPresentationStyle = UIModalPresentationStyle.Custom
}
第六步, 主控制器中加一个变量
// // 是否正在展现的标记
var isPresentation = false
第七步,实现主控制器中实现下面的代码
/// 视图控制器转场代理
extension HomeTableViewController: UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{ /// 返回控制 Modal 管理的控制器
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? { return HJCPopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
} /// 返回提供 Modal 展现动画对象
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { // Modal
isPresentation = true return self
} /// 返回提供 Dismiss 动画的对象
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { // Dismiss
isPresentation = false return self
} // MARK: - UIViewControllerAnimatedTransitioning
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return 0.5
} /**
transitionContext 转场上下文,提供转场动画的相关信息
从哪里来,到哪里去 主动提供转场的动画,一旦实现了这个方法,原有的转场动画会失效->目标控制器看不到了
*/
func animateTransition(transitionContext: UIViewControllerContextTransitioning) { // 将 toVC 的 view 添加到容器视图
if isPresentation {
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)! // 将目标视图添加到容器视图
transitionContext.containerView().addSubview(toView) // 动画方法
// 设置动画初始的形变
toView.transform = CGAffineTransformMakeScale(1.0, 0)
// 设置图层的锚点
toView.layer.anchorPoint = CGPointMake(0.5, 0) UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: nil, animations: { () -> Void in toView.transform = CGAffineTransformMakeScale(1.0, 1.0) }, completion: { (_) -> Void in
transitionContext.completeTransition(true)
})
} else {
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)! // 将 Modal 出来的视图 从容器视图中删除
fromView.removeFromSuperview() // 动画完成
// *** 一定必须要执行的函数,告诉系统转场动画结束了
transitionContext.completeTransition(true)
}
}
【Swift】UIPresentationController的使用方法的更多相关文章
- 【Swift学习】Swift编程之旅---方法(十五)
在Swift中结构体和枚举也能够定义方法,而在 Objective-C 中,类是唯一能定义方法的类型. 实例方法 实例方法是属于某个特定类.结构体或者枚举类型实例的方法,实例方法提供访问和修改实例属性 ...
- Swift编程语言中的方法引用
由于Apple官方的<The Swift Programming Guide>对Swift编程语言中的方法引用介绍得不多,所以这里将更深入.详细地介绍Swift中的方法引用. Swift与 ...
- Swift中自定义打印方法
// 1.获取打印所在的文件 let file = ( #file as NSString).lastPathComponent // 2.获取打印所在的方法 let funcName = #func ...
- iOS开发之swift与OC混编出现的坑,oc中不能对swift的代理进行调用,不能访问swift中的代理,swift中的回调方法
1. Swift与oc混编译具体怎么实现,这儿我就不重复讲出了,网上有大把的人讲解. 2. 在swift与OC混编的编译环境下, oc类不能访问swift创建类中的代理? 解决方法如下: 在代理的头部 ...
- Swift下表和方法
本文将同时阐述Swift的方法和下标,因为根据我的理解,从某种程度上讲,下标的本质也是方法. 方法 众所周知,在支持面向对象的语言里,方法一般分为两种:实例方法和类方法(在有的语言中,「类方法」被称为 ...
- Swift语言之类型方法
Swift语言有很多特性,其中之一就是类型方法,相对于其他比较流行的编程语言(C#.Java),在Swift中类型方法最大的特征在于它的可继承性,我们举个例子说明: 俗话说,龙生龙凤生凤老鼠生儿会打洞 ...
- Swift - AppDelegate.swift类中默认方法的介绍
项目创建后,AppDelegate类中默认带有如下几个方法,具体功能如下: 1,应用程序第一次运行时执行 这个方法只有在App第一次运行的时候被执行过一次,每次App从后台激活时都不会再执行该方法. ...
- Swift的属性,方法,下标脚本以及继承
从这篇章节起,Swift编程语言指南大部分的重要内容在于概念,代码并非太多.理解Swift的面向对象理念,语法以及类结构,构造析构过程对于非常好的应用Swift语言将会有比較大的帮助. 属性 存储属性 ...
- swift网络数据请求方法
搭建一个apache服务器,用php编写一个返回给客户端请求数据的脚本 <?php // header("Content-type:text/html;charset=utf-8&qu ...
随机推荐
- 几条常见的数据库分页 SQL 语句
SQL Server 先从想要的数据处理加上Row_number()来为数据的row加上一个RowNum作为有多少条数据,然后再用BETWEEN来分隔 with t1 as (select * , ...
- 数据库数据带&符号 导入有问题的处理办法
在sql文件头部加个: set feedback off set define off 我们在plsql里面将一条语句导出时会出现以下结果(测试表t_test): prompt Importing ...
- 实现win的on程序数据更新
枚举是一组描述性的名称定义一组有限的值,不包含方法对可能的值进行约束枚举是一组指定的常数,对可能的值进行约束枚举使用时直观方便.更易于维护 pictureBox控件属性名称 说明image ...
- Alpha第十天
Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- Beta版本敏捷冲刺每日报告——Day4
1.情况简述 Beta阶段第四次Scrum Meeting 敏捷开发起止时间 2017.11.5 08:00 -- 2017.11.5 22:00 讨论时间地点 2017.11.5晚9:00,软工所实 ...
- 冲刺NO.6
Alpha冲刺第六天 站立式会议 项目进展 项目中学生基本信息管理,与系统管理员模块基本完成,团队开始编写学生信用信息模块内容与奖惩事务管理内容,准备开始对已完成模块进行测试. 问题困难 团队成员对前 ...
- The sum of numbers form 0 to n.(20.9.2017)
#include <stdio.h> int main() { int a,b,sum; printf("输入一个数字: "); scanf("%d" ...
- bisect 二分查找
先说明的是,使用这个模块的函数前先确保操作的列表是已排序的. 先看看 insort 函数: 其插入的结果是不会影响原有的排序. 再看看 bisect 函数: 其目的在于查找该数值将会插入的位置并返 ...
- PostgreSQL 配置安装
Mac 安装 http://postgresapp.com/ 创建和删除数据库用户 对应命令如下(在postgres=# 环境下):1.查看数据库用户列表: \du2.创建数据库用户: create ...
- javascript递归函数
递归函数:是指函数直接或间接调用函数本身,则称该函数为递归函数. 这句话理解起来并不难,从概念上出发,给出以下的例子: function foo(){ console.log("函数 foo ...