实现思路
@available(iOS 7.0, *)
open var interactivePopGestureRecognizer: UIGestureRecognizer? { get }
iOS 7.0系统增加了滑动的pop手势,可惜的是响应范围太小,只是屏幕的边缘位置.
我们可以利用runtime
+KVC
来获取到系统的interactivePopGestureRecognizer
手势,并将其添加到view
上来实现全屏pop.
- 使用运行时,打印手势中所有属性,可以看到其中的
_targets
属性(具体实现中不需要此段代码)
var count : UInt32 = 0
let ivars = class_copyIvarList(UIGestureRecognizer.self, &count)!
for i in 0..<count {
let nameP = ivar_getName(ivars[Int(i)])!
let name = String(cString: nameP)
print(name)
}
- 使用KVC获取
interactivePopGestureRecognizer
的_targets
并打印第一个元素的属性
guard let targets = interactivePopGestureRecognizer!.value(forKey: "_targets") as? [NSObject] else {
return
}
let targetObjc = targets[0]
print(targetObjc)
- 最后利用获取到的
target
和action
创建手势添加到view
上
let target = targetObjc.value(forKey: "target")
let action = Selector(("handleNavigationTransition:"))
let panGes = UIPanGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(panGes)
完整代码
自定义UINavigationController
import UIKit
class SANNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
guard let targets = interactivePopGestureRecognizer!.value(forKey: "_targets") as? [NSObject] else {
return
}
let targetObjc = targets[0]
let target = targetObjc.value(forKey: "target")
let action = Selector(("handleNavigationTransition:"))
let panGes = UIPanGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(panGes)
}
}