只對(duì)目前需求進(jìn)行記錄,有問(wèn)題請(qǐng)大神指出
借鑒自分貝丶博客:http://blog.csdn.net/meiwenjie110/article/details/70331880
class BaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
/*
分析:一般用手勢(shì)觸發(fā)某個(gè)行為需要哪些條件?
1、需要?jiǎng)?chuàng)建一個(gè)我們需要的手勢(shì)實(shí)例;
2、添加到一個(gè)View上(需要一個(gè)view);
3、需要一個(gè)Target;
4、需要一個(gè)Action。
let tapG = UITapGestureRecognizer()
view.addGestureRecognizer(tapG)
tapG.addTarget(<#T##target: Any##Any#>, action: <#T##Selector#>)
我們需要改成全屏觸發(fā),其實(shí)Target和Action是不需要改的,那我們就先拿到Target和Action。
再拿到手勢(shì)和添加手勢(shì)的View,嘗試的去改手勢(shì)和添加手勢(shì)的View。
總結(jié):從iOS 7.0系統(tǒng)就幫我添加了手勢(shì)返回,但是只支持左邊緣觸發(fā),現(xiàn)在我們需要改成全屏觸發(fā),只要把系統(tǒng)的手勢(shì)更換為UIPanGestureRecognizer.
*/
//獲取系統(tǒng)的Pop手勢(shì)
guard let systemGes =self.navigationController?.interactivePopGestureRecognizer else{return}
print(systemGes)
//獲取系統(tǒng)手勢(shì)添加的view
guard let gesView = systemGes.view else{return}
//獲取Target和Action,但是系統(tǒng)并沒(méi)有暴露相關(guān)屬性
//利用class_copyIvarList查看所有的屬性(發(fā)現(xiàn)_targets是一個(gè)數(shù)組)
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查看所有的方法(并沒(méi)有找到我們想要的方法)
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}
//方法名稱(chēng)獲取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、由于在主頁(yè)面使用手勢(shì)會(huì)造成pushBUG,因此此處限制主頁(yè)面手勢(shì) 2、解決網(wǎng)頁(yè)向左劃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
}
}