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获得,并加以修改成自己的动画效果,测试可以正常工作。

相关推荐
凤枭香几秒前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
ULTRA??4 分钟前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
远望清一色21 分钟前
基于MATLAB的实现垃圾分类Matlab源码
开发语言·matlab
confiself30 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
XiaoLeisj42 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
杜杜的man1 小时前
【go从零单排】go中的结构体struct和method
开发语言·后端·golang
幼儿园老大*1 小时前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
半桶水专家1 小时前
go语言中package详解
开发语言·golang·xcode
llllinuuu1 小时前
Go语言结构体、方法与接口
开发语言·后端·golang
cookies_s_s1 小时前
Golang--协程和管道
开发语言·后端·golang