iOS全屏手势POP

开篇

在iOS中,默认如果有导航控制器,当push到下一个控制器时,当前界面时可以局部侧滑返回的,这一段范围很少.

既然系统已经给我们了实现好了,侧滑返回,那么我们如何解决,全屏手势侧滑返回呢?

方案

拿到系统的返回手势,操作一波

runtime 打印导航控制器所有私有属性

1
2
3
4
5
6
7
8
9
/// 通过runtime获取对应的手势属性
var count : UInt32 = 0
let ivars = class_copyIvarList(UIGestureRecognizer.self, &count)!
for i in 0..<count {
let ivar = ivars[Int(i)]
let namep = ivar_getName(ivar)
let name = String(cString: namep!)//属性名称
print(name)
}

打印结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
_gestureFlags
_targets // 这个保存手势的所有target
_delayedTouches
_delayedPresses
_view
_lastTouchTimestamp
_firstEventTimestamp
_state
_allowedTouchTypes
_initialTouchType
_internalActiveTouches
_forceClassifier
_requiredPreviewForceState
_touchForceObservable
_touchForceObservableAndClassifierObservation
_forceTargets
_forcePressCount
_beganObservable
_failureRequirements
_failureDependents
_activeEvents
_keepTouchesOnContinuation
_delegate
_allowedPressTypes
_name
_gestureEnvironment

获取target

1
2
3
4
5
6
guard let anyObject = interactivePopGestureRecognizer!.value(forKey:  "_targets") as? [AnyObject] else { 
return
}

let object = anyObject.first
print(object)

打印结果:

1
2
/// 原来是一个数组,数组中是一个tuple, tuple第一个是一个action,第二个既然是target对象
[(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fc664e4f850>)]

KVC 取出target

1
2
3
4
5
let target = object.value(forKeyPath: "target")
let action = Selector(("handleNavigationTransition:"))

//下面是获取不到action的会奔溃
//let action = object.value(forKeyPath: "action") as? Selector

替换手势

1
2
3
//创建自己的手势 将系统的手势去代替自己创建的手势
let panGes = UIPanGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(panGes)

使用FDFullscreenPopGesture

这玩意就补多少了,添加进去,啥都不用干,懒人方案,完美解决。

0%