Swift转场动画的使用
发布时间:2020-12-14 07:03:09 所属栏目:百科 来源:网络整理
导读:ViewController.swift主控制器 创建一个按钮,点击按钮时弹出新的控制器 //PopoverTableViewController:弹出控制器的名字 let viewController = PopoverTableViewController() //制定一个转场代理:popoverAnimator viewController.transitioningDelegate = po
ViewController.swift<主控制器>创建一个按钮,点击按钮时弹出新的控制器//PopoverTableViewController:弹出控制器的名字
let viewController = PopoverTableViewController()
//制定一个转场代理:popoverAnimator
viewController.transitioningDelegate = popoverAnimator
//设置转场样式:自定义
viewController.modalPresentationStyle = UIModalPresentationStyle.Custom
//动画弹出菜单
presentViewController(viewController,animated: true,completion: nil)
//懒加载转场
private lazy var popoverAnimator: PopoverAnimator = {
let popoverAnimator = PopoverAnimator()
//在这里设置弹出菜单的位置和大小
popoverAnimator.presentFrame = CGRectMake(UIScreen.mainScreen().bounds.size.width / 2 - 100,56,200,350)
return popoverAnimator
}()
PopoverAnimator.Swift<转场代理>//实现代理方法,告诉系统谁来负责转场动画
func presentationControllerForPresentedViewController(presented: UIViewController,presentingViewController presenting: UIViewController,sourceViewController source: UIViewController) -> UIPresentationController? {
let pc = PopoverPresentAtionController(presentedViewController: presented,presentingViewController: presenting)
//设置菜单的大小
pc.presrntFrame = presentFrame
return pc
}
//只要实现了以下方法,系统默认的动画效果就没有了,需要自己实现
func animationControllerForPresentedController(presented: UIViewController,presentingController presenting: UIViewController,sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//展现动画执行的操作
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//消失动画执行的操作
return self
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
//动画时长
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,presentingViewController: UIViewController) {
super.init(presentedViewController: presentedViewController,presentingViewController: presentingViewController)
}
/** 重写containerViewWillLayoutSubviews,在即将布局转场子视图时调用 */
override func containerViewWillLayoutSubviews() {
//修改弹出视图的大小
//presentedView: 被展现的视图
//containerView: 容器视图
}
/// 懒加载蒙版效果
private lazy var converView: UIView = {
//创建蒙版
let view = UIView()
view.backgroundColor = UIColor(white: 0.0,alpha: 0.3)
view.frame = UIScreen.mainScreen().bounds
//为蒙版view添加一个监听,点击蒙版的时候,转场消失
let tap = UITapGestureRecognizer(target: self,action: #selector(PopoverPresentAtionController.close))
view.addGestureRecognizer(tap)
return view
}()
///关闭菜单
func close() {
presentedViewController.dismissViewControllerAnimated(true,completion: nil)
}
PopoverTableViewController.swift是弹出的菜单,自己在里面布局demo下载:https://github.com/1170197998/transitioningAnimation(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |