iOS Swift开发 navigation模式自定义pop和push动画

在基于navigation的跳转涉及到pop和push操作,这个时候,如果想要自定义画面切入的动画可以按如下操作

添加如下类:

Swift 复制代码
import UIKit

class SimpleOver: NSObject, UIViewControllerAnimatedTransitioning {
        
        var popStyle: Bool = false
        
        func transitionDuration(
            using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
            return 0.20
        }
        
        func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
            
            if popStyle {
                animatePop(using: transitionContext)
                return
            }
            
            let fz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
            let tz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
            
            let f = transitionContext.finalFrame(for: tz)
            
            let fOff = f.offsetBy(dx: 0, dy: f.height)
            //let fOff = f.offsetBy(dx: f.width, dy: 0)
            tz.view.frame = fOff
            
            transitionContext.containerView.insertSubview(tz.view, aboveSubview: fz.view)
            
            UIView.animate(
                withDuration: transitionDuration(using: transitionContext),
                animations: {
                    tz.view.frame = f
            }, completion: {_ in
                    transitionContext.completeTransition(true)
            })
        }
        
        func animatePop(using transitionContext: UIViewControllerContextTransitioning) {
            
            let fz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
            let tz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
            
            let f = transitionContext.initialFrame(for: fz)
            let fOffPop = f.offsetBy(dx: 0, dy: f.height)
            //let fOffPop = f.offsetBy(dx: f.width, dy: 0)
            
            transitionContext.containerView.insertSubview(tz.view, belowSubview: fz.view)
            
            UIView.animate(
                withDuration: transitionDuration(using: transitionContext),
                animations: {
                    fz.view.frame = fOffPop
            }, completion: {_ in
                    transitionContext.completeTransition(true)
            })
        }
    }

在第一个载入的界面中添加

Swift 复制代码
class ViewController: UIViewController,UIViewControllerTransitioningDelegate, UINavigationControllerDelegate {
    let simpleOver = SimpleOver()
    ...
    func navigationController(
        _ navigationController: UINavigationController,
        animationControllerFor operation: UINavigationController.Operation,
        from fromVC: UIViewController,
        to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        simpleOver.popStyle = (operation == .pop)
        return simpleOver
    }
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        navigationController?.delegate = self
        ...
    }
    ...
}

之后正常使用pop和push命令,即可使用我们自定义的动画效果了。

此方法从stackoverflow获得,并加以修改成自己的动画效果,测试可以正常工作。

相关推荐
禁默10 分钟前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Code哈哈笑19 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
程序猿进阶23 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
qq_4336184425 分钟前
shell 编程(二)
开发语言·bash·shell
charlie11451419140 分钟前
C++ STL CookBook
开发语言·c++·stl·c++20
袁袁袁袁满40 分钟前
100天精通Python(爬虫篇)——第113天:‌爬虫基础模块之urllib详细教程大全
开发语言·爬虫·python·网络爬虫·爬虫实战·urllib·urllib模块教程
ELI_He9991 小时前
PHP中替换某个包或某个类
开发语言·php
m0_748236111 小时前
Calcite Web 项目常见问题解决方案
开发语言·前端·rust
东坡肘子1 小时前
肘子的 Swift 周报 #063|异种肾脏移植取得突破
swiftui·swift·apple
倔强的石头1061 小时前
【C++指南】类和对象(九):内部类
开发语言·c++