我有一个UIViewController
被推送到导航堆栈。我想扩展标准的iOS7交互式平移手势,使此视图控制器超出默认的UIRectEdgeLeft
边界,这样用户就可以通过从视图中的任何位置进行平移来启动交互式后退操作。
我已经尝试过滚动我自己的交互式视图控制器转换,但完全复制默认interactivePopGestureRecognizer
的漂亮视差处理是很麻烦的。例如,fromViewController
隐藏导航栏,而toViewController
显示它 - 在自定义交互式转换中不易处理,但在默认操作中无缝。
因此,我想将默认操作扩展到更大的平移手势区域,但API似乎不支持简单地替换手势。
有任何创意建议吗?
答案 0 :(得分:4)
查看我的库SloppySwiper,通过使用UIPanGestureRecognizer
并重新创建默认动画来实现此目的。您还可以在https://github.com/fastred/SloppySwiper/issues/1中查看我的想法。
答案 1 :(得分:1)
实际上,在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
}
}