Swift 原生返回手势和全屏返回手势

2024-03-21  本文已影响0人  jsone

简介

这里用到的返回手势和全屏返回手势的功能由 YDRootNavigationController 实现的,可以查看 Swift 全局默认导航栏样式与视图控制器自定义并存 这篇文章关于它的详细介绍。

一、全屏返回手势

全局默认设置

1.创建一个类用来实现YDAppAppearanceProtocol协议

class MyAppAppearance: YDAppAppearanceProtocol {
    // 如果不实现该计算属性,默认是开启
    var isInteractivePopGestureEnabled: Bool { false }
}

2.在AppDelegate中调用

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // 全局默认样式配置
        MyAppAppearance().configure()
        return true
    }
}

3.导航栏控制器设置为YDRootNavigationController或继承YDRootNavigationController的类

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window?.rootViewController = YDRootNavigationController()
    return true
}

视图控制器默认设置

class ViewController: UIViewController {
    // 默认设置
    override var isInteractivePopGestureEnabled: Bool { false }
}

视图控制器中动态设置

class ViewController: UIViewController {
    // 默认设置
    override var isInteractivePopGestureEnabled: Bool { interactivePopGesture }
    var interactivePopGesture: Bool = true {
        didSet {
            // 动态设置
            interactivePopGesture(interactivePopGesture)
        }
    }
}
返回手势.gif

二、全屏返回手势

全局默认设置

同返回手势设置方法一致,对应属性名称改为isFullScreenPopGestureEnabled即可

视图控制器默认设置

class ViewController: UIViewController {
    // 默认设置
    override var isFullScreenPopGestureEnabled: Bool { false }
}

视图控制器中动态设置

class ViewController: UIViewController {
    // 默认设置
    override var isFullScreenPopGestureEnabled: Bool { fullScreenPopGesture }
    var fullScreenPopGesture: Bool = true {
        didSet {
            // 动态设置
            fullScreenPopGesture(fullScreenPopGesture)
        }
    }
}
全屏返回手势.gif
上一篇 下一篇

猜你喜欢

热点阅读