滑动返回仅适用于屏幕边缘?
Swipe to go back only works on edge of screen?
我的滑动返回功能有效,但仅在屏幕边缘有效。我怎样才能让它在屏幕上的任何地方工作?
滑动返回是 pushed/showed 视图控制器的默认行为。它从屏幕的左边缘开始工作(默认情况下)。如果您想从屏幕的任何部分向后滑动,您应该将 UISwipeGestureRecognizer 添加到您的视图中:
let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "didSwipe:")
self.view.addGestureRecognizer(swipeGestureRecognizer)
Apple 说 here :
interactivePopGestureRecognizer
The gesture recognizer responsible for popping the top view controller
off the navigation stack. (read-only)
@property(nonatomic, readonly) UIGestureRecognizer
*interactivePopGestureRecognizer
The navigation controller installs this gesture recognizer on its view
and uses it to pop the topmost view controller off the navigation
stack. You can use this property to retrieve the gesture recognizer
and tie it to the behavior of other gesture recognizers in your user
interface. When tying your gesture recognizers together, make sure
they recognize their gestures simultaneously to ensure that your
gesture recognizers are given a chance to handle the event.
因此 SloppySwiper 库自定义 UIPanGestureRecognizer
。
查看库 SloppySwiper,它通过使用 UIPanGestureRecognizer 和重新创建默认动画来实现这一点。
SloppySwiper:-
UINavigationController 委托,允许从屏幕上的任何位置启动向后滑动手势,例如 instagram。
这个库的用法可以找到here.
Cocoapods:- pod "SloppySwiper"
我在 ios7 及更高版本上测试了这个库。它就像一个魅力。
实际上在 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
}
}
我的滑动返回功能有效,但仅在屏幕边缘有效。我怎样才能让它在屏幕上的任何地方工作?
滑动返回是 pushed/showed 视图控制器的默认行为。它从屏幕的左边缘开始工作(默认情况下)。如果您想从屏幕的任何部分向后滑动,您应该将 UISwipeGestureRecognizer 添加到您的视图中:
let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "didSwipe:")
self.view.addGestureRecognizer(swipeGestureRecognizer)
Apple 说 here :
interactivePopGestureRecognizer
The gesture recognizer responsible for popping the top view controller off the navigation stack. (read-only)
@property(nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer
The navigation controller installs this gesture recognizer on its view and uses it to pop the topmost view controller off the navigation stack. You can use this property to retrieve the gesture recognizer and tie it to the behavior of other gesture recognizers in your user interface. When tying your gesture recognizers together, make sure they recognize their gestures simultaneously to ensure that your gesture recognizers are given a chance to handle the event.
因此 SloppySwiper 库自定义 UIPanGestureRecognizer
。
查看库 SloppySwiper,它通过使用 UIPanGestureRecognizer 和重新创建默认动画来实现这一点。
SloppySwiper:-
UINavigationController 委托,允许从屏幕上的任何位置启动向后滑动手势,例如 instagram。
这个库的用法可以找到here.
Cocoapods:- pod "SloppySwiper"
我在 ios7 及更高版本上测试了这个库。它就像一个魅力。
实际上在 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
}
}