Man*_*uel 10 uinavigationcontroller uigesturerecognizer swift
在我UINavigationController添加的自定义后退按钮中,副作用是不再可以从左向右滑动以弹出视图控制器并导航回来.
所以我interactivePopGestureRecognizer在我的自定义UINavigationController类中实现:
class UINavigationControllerExtended: UINavigationController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if self.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
self.interactivePopGestureRecognizer?.delegate = self
}
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailByGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer)
}
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,除非我在我的根视图控制器(RVC)中,它是一个UICollectionViewController,即导航堆栈中最底层的视图控制器.当我从左到右滑动手势时,似乎没有任何事情发生,正如预期的那样.但是当我点击UICollectionViewCell时,目标视图控制器(DVC)不会被推过RVC.相反,我只在屏幕右侧看到DVC的阴影.
RVC不再响应,但当我再次从左向右滑动时,DVC以交互方式从右向左移动到屏幕中.当我完成手势时,DVC完全移动到屏幕中,以便从左到右快速消失.RVC再次响应.
所以似乎DVC被推到了导航堆栈上,但没有明显地进入屏幕.
这个奇怪的行为起源的任何建议?
我目前的解决方案是interactivePopGestureRecognizer在根视图控制器中禁用它:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.enabled = false
}
Run Code Online (Sandbox Code Playgroud)
在第一个子视图控制器中,我再次启用它.但这似乎更像是一种解决方法,因为我不了解导航堆栈首先搞砸的实际问题.
UINavigationControllerDelegate为您的导航控制器实施,并在那里启用/禁用手势识别器。
// Fix bug when pop gesture is enabled for the root controller
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
self.interactivePopGestureRecognizer?.enabled = self.viewControllers.count > 1
}
Run Code Online (Sandbox Code Playgroud)
保持代码独立于推送视图控制器。
这是我的解决方案。斯威夫特 4.2
实施UIGestureRecognizerDelegate和UINavigationControllerDelegate协议。在我的情况下,我在这UITabBarController也是我的根视图控制器中做到了这一点。
然后,在 viewDidLoad 上,执行:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
self.navigationController?.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
然后,添加委托方法UINavigationControllerDelegate并通过计算导航控制器上的视图数量来检查它是否是根视图,如果是则禁用它,如果不是则启用它。
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
let enable = self.navigationController?.viewControllers.count ?? 0 > 1
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = enable
}
Run Code Online (Sandbox Code Playgroud)
最后,添加UIGestureRecognizerDelegate方法
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题,而无需在项目的每个视图中手动启用/禁用手势识别器。
| 归档时间: |
|
| 查看次数: |
4763 次 |
| 最近记录: |