前端之家收集整理的这篇文章主要介绍了
Swift转场动画的使用,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
ViewController.swift<主控制器>
创建一个按钮,点击按钮时弹出新的控制器
let viewController = PopoverTableViewController()
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
/** 重写初始化方法,用于创建负责转场的动画 - parameter presentedViewController: 被展现的控制器 - parameter presentingViewController: 发起的控制器 */
override init(presentedViewController: UIViewController,presentingViewController: UIViewController) {
super.init(presentedViewController: presentedViewController,presentingViewController: presentingViewController)
}
/** 重写containerViewWillLayoutSubviews,在即将布局转场子视图时调用 */
override func containerViewWillLayoutSubviews() {
}
private lazy var converView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0.0,alpha: 0.3)
view.frame = UIScreen.mainScreen().bounds
let tap = UITapGestureRecognizer(target: self,action: #selector(PopoverPresentAtionController.close))
view.addGestureRecognizer(tap)
return view
}()
func close() {
presentedViewController.dismissViewControllerAnimated(true,completion: nil)
}
PopoverTableViewController.swift是弹出的菜单,自己在里面布局