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

简介

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

UIScrollView与返回手势.gif

地图与返回手势.gif

一、全屏返回手势

  • 全局默认设置
  • 视图控制器默认设置
  • 视图控制器中动态设置

全局默认设置

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
}
  • Interface Builder设置
    IB设置YDRootNavigationController.gif

视图控制器默认设置

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容