利用runtime获取系统自带的pop手势,实现全屏pop

实现思路

@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..
  • 使用KVC获取interactivePopGestureRecognizer_targets并打印第一个元素的属性
guard let targets = interactivePopGestureRecognizer!.value(forKey: "_targets") as? [NSObject] else {
       return
}
let targetObjc = targets[0]
print(targetObjc)
利用runtime获取系统自带的pop手势,实现全屏pop_第1张图片
上述代码两次的打印结果@2x.png
  • 最后利用获取到的targetaction创建手势添加到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) 
    }
}

你可能感兴趣的:(利用runtime获取系统自带的pop手势,实现全屏pop)