只对目前需求进行记录,有问题请大神指出
借鉴自分贝丶博客:http://blog.csdn.net/meiwenjie110/article/details/70331880
class BaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
/*
分析:一般用手势触发某个行为需要哪些条件?
1、需要创建一个我们需要的手势实例;
2、添加到一个View上(需要一个view);
3、需要一个Target;
4、需要一个Action。
let tapG = UITapGestureRecognizer()
view.addGestureRecognizer(tapG)
tapG.addTarget(<#T##target: Any##Any#>, action: <#T##Selector#>)
我们需要改成全屏触发,其实Target和Action是不需要改的,那我们就先拿到Target和Action。
再拿到手势和添加手势的View,尝试的去改手势和添加手势的View。
总结:从iOS 7.0系统就帮我添加了手势返回,但是只支持左边缘触发,现在我们需要改成全屏触发,只要把系统的手势更换为UIPanGestureRecognizer.
*/
//获取系统的Pop手势
guard let systemGes =self.navigationController?.interactivePopGestureRecognizer else{return}
print(systemGes)
//获取系统手势添加的view
guard let gesView = systemGes.view else{return}
//获取Target和Action,但是系统并没有暴露相关属性
//利用class_copyIvarList查看所有的属性(发现_targets是一个数组)
print("------------------------属性---------------------------------")
varivarCount :UInt32=0
let ivars =class_copyIvarList(UIGestureRecognizer.self, &ivarCount)!
for i in0..
letivar = ivars[Int(i)]
letname =ivar_getName(ivar)
print(String(cString: name!))
}
print("------------------------方法---------------------------------")
//利用class_copyMethodList查看所有的方法(并没有找到我们想要的方法)
var methodCount :UInt32=0
let methods =class_copyMethodList(UIGestureRecognizer.self, &methodCount)!
for i in0..< methodCount {
let method = methods[Int(i)]
let name =method_getName(method)
print("\(name)")
}
//从Targets取出Target
let targets = systemGes.value(forKey:"_targets")as? [NSObject]
guard let targetObjc = targets?.first else {return}
guard let target = targetObjc.value(forKey:"target") else{return}
//方法名称获取Action
let action =Selector(("handleNavigationTransition:"))
let panGes =UIPanGestureRecognizer()
panGes.delegate = self
gesView.addGestureRecognizer(panGes)
panGes.addTarget(target, action: action)
// MARK: - UIGestureRecognizerDelegate
extension BaseNavigationController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
//1、由于在主页面使用手势会造成pushBUG,因此此处限制主页面手势 2、解决网页向左划BUG
guard let viewControllers = self.navigationController?.viewControllers else {
return false
}
if (viewControllers.last?.isKind(of: WKWebViewController.classForCoder()))!, let ges = gestureRecognizer as? UIPanGestureRecognizer {
if ges.translation(in: self.view).x < 0 {//<0向左划
return false
}
}
return viewControllers.count > 1
}
}