滑动返回仅适用于屏幕边缘?

4 storyboard ios uistoryboard swift ios9

我的滑动返回功能有效,但仅适用于屏幕边缘。我怎样才能让它在屏幕上的任何地方工作?

rsh*_*hev 14

UINavigationController子类上做实际上很容易,而无需对UIViewController推送的每个子类进行任何干预。还尊重内置的从边缘滑动状态(所以当它被有意禁用时,新手势也会被禁用):

import UIKit

class NavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupFullWidthBackGesture()
    }

    private lazy var fullWidthBackGestureRecognizer = UIPanGestureRecognizer()

    private func setupFullWidthBackGesture() {
        // The trick here is to wire up our full-width `fullWidthBackGestureRecognizer` to execute the same handler as
        // the system `interactivePopGestureRecognizer`. That's done by assigning the same "targets" (effectively
        // object and selector) of the system one to our gesture recognizer.
        guard
            let interactivePopGestureRecognizer = interactivePopGestureRecognizer,
            let targets = interactivePopGestureRecognizer.value(forKey: "targets")
        else {
            return
        }

        fullWidthBackGestureRecognizer.setValue(targets, forKey: "targets")
        fullWidthBackGestureRecognizer.delegate = self
        view.addGestureRecognizer(fullWidthBackGestureRecognizer)
    }
}

extension NavigationController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        let isSystemSwipeToBackEnabled = interactivePopGestureRecognizer?.isEnabled == true
        let isThereStackedViewControllers = viewControllers.count > 1
        return isSystemSwipeToBackEnabled && isThereStackedViewControllers
    }
}
Run Code Online (Sandbox Code Playgroud)


pkc*_*456 3

苹果在这里说:

交互式流行手势识别器

手势识别器负责将顶视图控制器从导航堆栈中弹出。(只读)

@property(非原子,只读) UIGestureRecognizer *interactivePopGestureRecognizer

导航控制器在其视图上安装此手势识别器,并使用它从导航堆栈中弹出最顶层的视图控制器。您可以使用此属性来检索手势识别器并将其与用户界面中其他手势识别器的行为联系起来。将手势识别器绑定在一起时,请确保它们同时识别自己的手势,以确保您的手势识别器有机会处理该事件。

因此SloppySwiper库自定义了UIPanGestureRecognizer.

查看库SloppySwiper,它通过使用 UIPanGestureRecognizer 并重新创建默认动画来实现此目的。

SloppySwiper:-UINavigationController 委托,允许从屏幕上的任何位置(例如 Instagram)启动向后滑动手势。

可以在此处找到该库的用法

Cocoapods:- pod“SloppySwiper”

我在ios7及以上版本上测试了这个库。它就像一个魅力。