全屏返回手势建议直接使用框架
FDFullscreenPopGesture
1.先添加一个代理类
fileprivate class FullscreenPopGestureRecognizerDelegate : NSObject, UIGestureRecognizerDelegate {
weak var navigationController: UINavigationController!
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
guard let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer else {
return false
}
if navigationController.viewControllers.count <= 1 {
return false
}
if let _isTransitioning = navigationController.value(forKey: "_isTransitioning") as? Bool, _isTransitioning {
return false
}
let translation = gestureRecognizer.translation(in: gestureRecognizer.view!)
let isLeftToRight = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight
let multiplier: CGFloat = isLeftToRight ? 1 : -1;
if translation.x * multiplier <= 0 {
return false
}
return true
//可以自己添加某一个控制器的禁止滑动返回手势
}
}
2.添加全屏手势方法
class MTNavigationController: QMUINavigationController {
private var fullScreenGesture: UIPanGestureRecognizer!
private let fullScreenGestureRecognizerDelegate = FullscreenPopGestureRecognizerDelegate()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addFullScreenGesture()
}
private func addFullScreenGesture() {
if fullScreenGesture == nil {
fullScreenGesture = UIPanGestureRecognizer()
fullScreenGesture.maximumNumberOfTouches = 1
fullScreenGestureRecognizerDelegate.navigationController = self
fullScreenGesture.delegate = fullScreenGestureRecognizerDelegate
//使用QMUINavigationViewController 时添加
let qmuiNav_sel = NSSelectorFromString("handleInteractivePopGestureRecognizer:")
if self.responds(to: qmuiNav_sel) {
fullScreenGesture.addTarget(self, action: qmuiNav_sel)
}
}
if let sysGes = self.interactivePopGestureRecognizer, let sysV = sysGes.view {
if !sysV.gestureRecognizers!.contains(fullScreenGesture) {
if let internalTargets = sysGes.value(forKey: "targets") as? [AnyObject],
internalTargets.count > 0,
let internalTarget = internalTargets.first!.value(forKey: "target") {
sysV.addGestureRecognizer(fullScreenGesture)
let internalAction = NSSelectorFromString("handleNavigationTransition:")
fullScreenGesture.addTarget(internalTarget, action: internalAction)
sysGes.isEnabled = false
}
}
}
}
}
如果有的控制器里面有横向滚动的ScrollView导致返回手势冲突的,(例如当前页面用了WMPageViewController或者UIPageViewController)再返回时禁用掉当前页面的手势,我这里的判断是:
这个方法[self.panGestureRecognizer translationInView:gestureRecognizer.view].x 的x值,向左滑动是正值,向右滑动是负值,然后再判断当前偏移量
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.panGestureRecognizer) {
BOOL isLeftToRight = [self.panGestureRecognizer translationInView:gestureRecognizer.view].x >= 0;
if (isLeftToRight && self.contentOffset.x == 0) {
return NO;
}
}
return YES;
}
网友评论