- 刚开始学习 swift 一段时间,学习果然还是还是要不停地敲代码的啊,全部代码都写在导航控制器 UINavigationController 的类里面。先说下 swift 的整个控制器的侧滑返回实现
- swift
- 1,第一种实现方法
// 获取手势属性
/*
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)//打印出所有属性
}
free(ivars);
*/
//判断并获取 targets 的 action
guard let targets = interactivePopGestureRecognizer!.value(forKey: "_targets") as? [NSObject] else{return}
let targetObjc = targets[0]
let target = targetObjc.value(forKey: "target")
//由于直接使用它的 action 会崩溃,所以自己需要重新构建一个 Selector,自定义一个手势
let action = Selector(("handleNavigationTransition:"))
let panGes = UIPanGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(panGes)
interactivePopGestureRecognizer!.delegate = self as? UIGestureRecognizerDelegate
object_setClass(interactivePopGestureRecognizer!.self, UIPanGestureRecognizer.classForCoder())
- OC 实现侧滑(实现其实和 swift 差不多)
- 1,第一种实现方法
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIGestureRecognizer class], &count);
//获取手势的属性变量
for (int i = 0; i<count; i++) {
NSString *str = [NSString stringWithCString:ivar_getName(ivars[(int)i]) encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
free(ivars);
id targets = [self.interactivePopGestureRecognizer valueForKey:@"_targets"];
NSLog(@"%@",targets);
id target = [targets[0] valueForKey:@"target"];
//更具 target 获取 selector
SEL selector = NSSelectorFromString(@"handleNavigationTransition:");
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:selector];
[self.view addGestureRecognizer:pan];
// 更换interactivePopGestureRecognizer的delegate为navigationController自己
self.interactivePopGestureRecognizer.delegate = self;
// 自带的边缘返回手势UIScreenEdgePanGestureRecognizer,但只负责edge部分。换成全屏的UIPanGestureRecognizer
object_setClass(self.interactivePopGestureRecognizer, [UIPanGestureRecognizer class]);
网友评论