您的位置:首页 > 移动开发 > Swift

Swift3.0转场动画的使用

2016-04-27 11:49 471 查看

ViewController.swift<主控制器>

创建一个按钮,点击按钮时弹出新的控制器

//PopoverTableViewController:弹出控制器的名字
let viewController = PopoverTableViewController()
//制定一个转场代理:popoverAnimator
viewController.transitioningDelegate = popoverAnimator
//设置转场样式:自定义
viewController.modalPresentationStyle = UIModalPresentationStyle.custom
//动画弹出菜单
present(viewController, animated: true, completion: nil)

//懒加载转场
private lazy var popoverAnimator: PopoverAnimator = {

let popoverAnimator = PopoverAnimator()
//在这里设置弹出菜单的位置和大小
pa.presentFrame = CGRect(x: UIScreen.main.bounds.size.width / 2 - 100, y: 56, width: 200, height: 350)
return popoverAnimator
}()


PopoverAnimator.Swift<转场代理>

//实现代理方法,告诉系统谁来负责转场动画
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {

let pc = PopoverPresentAtionController(presentedViewController: presented,presenting: presenting)
//设置菜单的大小
pc.presrntFrame = presentFrame
return pc
}

//只要实现了以下方法,系统默认的动画效果就没有了,需要自己实现
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

//展现动画执行的操作
return self
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

//消失动画执行的操作
return self
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
//动画时长
return 0.25
}


PopoverPresentAtionController.swift<管理弹出>

继承于: UIPresentationController

所有的UIViewController的presentation都是由UIPresentationController管理的。在UIPresentationController中可以定义content和chrome的动画,可以根据大小的变化来改变content大小,可以根据系统的不同,来改变展示方式,UIPresentationController也是可复用的,可以很容易的拿到其他的UIViewController中去使用。

弹出的,可以和用户交互的Controller叫做PresentedViewController,而后面那个被部分遮挡的UIViewController叫做PresentingViewController,而在UIPresentationController中,PresentedViewController是presentation的content,而PresentingViewController叫做Chrome

摘自:http://www.15yan.com/story/jlkJnPmVGzc/

/**
重写初始化方法,用于创建负责转场的动画
- parameter presentedViewController:  被展现的控制器
- parameter presentingViewController: 发起的控制器
*/
override init(presentedViewController: UIViewController, presenting: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presenting)
}

/**
重写containerViewWillLayoutSubviews,在即将布局转场子视图时调用
*/
override func containerViewWillLayoutSubviews() {

//修改弹出视图的大小
//presentedView: 被展现的视图
//containerView: 容器视图
}
/// 懒加载蒙版效果
fileprivate lazy var converView: UIView = {

//创建蒙版
let view = UIView()
view.backgroundColor = UIColor(white: 0.0, alpha: 0.3)
view.frame = UIScreen.main.bounds

//为蒙版view添加一个监听,点击蒙版的时候,转场消失
let tap = UITapGestureRecognizer(target: self,action: #selector(PopoverPresentAtionController.close))
view.addGestureRecognizer(tap)
return view
}()

///关闭菜单
func close() {
presentedViewController.dismiss(animated: true, completion: nil)
}


PopoverTableViewController.swift是弹出的菜单,自己在里面布局



DEMO下载:https://github.com/1170197998/Swift-DEMO

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swift 转场 下拉菜单