当前位置: 首页 > news >正文

UINavigationController内的页面跳转实现 UIViewController 的 present和dismiss动画

UINavigationController内部页面跳转默认为左右切换,但是当我们想向上弹出进入界面,或者向下离开界面时,需要实现UINavigationControllerDelegate 协议自行控制页面的动画(否则直接在navVc上叠加动画会导致动画结束后的那个页面,自动加了异常动画),本文介绍这个实现方案。

定义一个类实现 UIViewControllerAnimatedTransitioning协议,实现下面的函数:


class NavigationControllerAnimation: NSObject, UIViewControllerAnimatedTransitioning {let operation: UINavigationController.Operationinit(operation: UINavigationController.Operation) {self.operation = operationsuper.init()}//页面过渡动画时间 func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {return 0.3}//加页面过渡的动画public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {guard let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) else { return }let containerView = transitionContext.containerViewif operation == .push {// do your animation for pushtoViewController.view.frame = containerView.bounds.offsetBy(dx: 0, dy: containerView.frame.size.height)fromViewController.view.frame = containerView.boundscontainerView.addSubview(toViewController.view)UIView.animate(withDuration: transitionDuration(using: transitionContext),delay: 0,options: [ UIView.AnimationOptions.curveEaseOut ],animations: {toViewController.view.frame = containerView.bounds},completion: { (finished) intransitionContext.completeTransition(true)})} else if operation == .pop {// do your animation for popcontainerView.addSubview(toViewController.view)containerView.addSubview(fromViewController.view)//containerView 上加的view在动画结束后一段时间后被释放了fromViewController.view.frame  = containerView.bounds
//            toViewController.view.frame = containerView.boundsUIView.animate(withDuration: transitionDuration(using: transitionContext),animations: {fromViewController.view.frame = containerView.bounds.offsetBy(dx: 0, dy: containerView.frame.size.height)},completion: { (finished) intransitionContext.completeTransition(true)})}}
}

UINavigationController 添加delegate


class BaseNavigationController: UINavigationController {override func viewDidLoad() {super.viewDidLoad()self.delegate = self}
}

实现delegate的协议 ,返回本文最开始定义的类的对象,这里只要返回nil 就会是默认的动画方式(所以可以按照需求切换界面过渡动画)

extension BaseNavigationController : UINavigationControllerDelegate {func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {return NavigationControllerAnimation(operation: operation)  }}

参考:
https://stackoverflow.com/questions/2215672/how-to-change-the-push-and-pop-animations-in-a-navigation-based-app

http://www.lryc.cn/news/170276.html

相关文章:

  • PMP对项目管理工作有什么用?
  • Python 将‘20230919182550‘ 转换为 ‘%Y年%m月%d日 %H:%M‘
  • vue2.0检测无用的代码并删除
  • 小米华为,化干戈为玉帛!
  • 【文末赠书】SRE求职必会 —— 可观测性平台可观测性工程(Observability Engineering)
  • content生成自定义图标的方式是什么?
  • 无涯教程-JavaScript - SECH函数
  • 天宇微纳芯片ic测试软件如何测试芯片上下电功能?
  • 1万多爱背句子英语口语ACCESS\EXCEL数据库
  • C++:new 和 delete
  • mysql5.7版本的数据导入到mysql8.0版本需要怎么做
  • Python150题day06
  • 2023Node.js零基础教程(小白友好型),nodejs新手到高手,(一)NodeJS入门
  • 拉格朗日乘子法思路来源
  • 天选之子C++是如何发展起来的?如何学习C++呢?
  • Oracle Schema Only账户
  • 分界线-积木游戏 demo
  • 智能指针解读(2)
  • javax.servlet.ServletException: 非法访问资源(/j_spring_security_check)
  • 自定义事件的使用
  • buuctf-[ASIS 2019] Unicorn shop
  • 72.Linux系统下printf函数的输出问题
  • Ubuntu20.4搭建基于iRedMail的邮件服务器
  • 大数据-Spark-Spark开发高频面试题
  • 云原生容器平台——新华资产数字化转型加速器
  • ubuntu 22.04运行opencv4的c++程序遇到的问题
  • MDPI模板报错的问题---提示缺少sty文件
  • 【教程】微信小程序导入外部字体详细流程
  • 关于Kali部署OneForAll,不能运行问题
  • vue3中使用el-upload + tui-image-editor进行图片处理