最近工作陆续生产了一些方便开发的工具类,尽管最终没被收入使用,但不妨碍个人使用,故特此开一篇博文,也记录一些自己踩的坑。
简单来说,你可以这样使用 UIGestureRecognizer:
[self.view addGestureRecognizer:[UITapGestureRecognizer gestureRecognizerWithActionBlock:^(id gestureRecognizer) { //... }]];
不再需要繁琐地使用 selector 反射,也解决了代码分离的问题。 实现代码如下:
static const int target_key; @implementation UIGestureRecognizer (Block) +(instancetype)nvm_gestureRecognizerWithActionBlock:(NVMGestureBlock)block { return [[self alloc]initWithActionBlock:block]; } - (instancetype)initWithActionBlock:(NVMGestureBlock)block { self = [self init]; [self addActionBlock:block]; [self addTarget:self action:@selector(invoke:)]; return self; } - (void)addActionBlock:(NVMGestureBlock)block { if (block) { objc_setAssociatedObject(self, ⌖_key, block, OBJC_ASSOCIATION_COPY_NONATOMIC); } } - (void)invoke:(id)sender { NVMGestureBlock block = objc_getAssociatedObject(self, ⌖_key); if (block) { block(sender); } } @end
原理就是把 block 动态地绑成 UIGestureRecognizer 的一个变量,invoke 的时候再调用这个 block。
开发过程中遇到了两个坑。
//以下是错误代码: + (instancetype)initWithActionBlock:(KYGestureBlock)block { return [[self alloc] initWithTarget: [self _gestureRecognizerBlockTarget:block] selector:@selector(invoke:)]; } + (_KYGestureRecognizerBlockTarget *)_gestureRecognizerBlockTarget:(KYGestureBlock)block{ _KYGestureRecognizerBlockTarget *target = objc_getAssociatedObject(self, ⌖_key); if (!target) { target = [[_KYGestureRecognizerBlockTarget alloc]initWithBlock:block]; objc_setAssociatedObject(self, ⌖_key, target, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } return target; }
这样导致的结果就是,变量被绑到了这个类对象上,因为在类方法里面 self 指的是这个类对象,而类对象是常驻内存直到程序退出才释放的,这也就导致了这个类上始终绑着第一次的 target,之后无论怎样都不会改变。如果恰好你在 block 中有强制持有了 block 外的其他对象,那就会导致这些对象都不会释放,造成内存泄露。在实例方法中动态绑定即可解决。
//错误代码 + (instancetype)initWithActionBlock:(KYGestureBlock)block { return [[self alloc] initWithTarget: [[_KYGestureRecognizerBlockTarget alloc]initWithBlock:block] selector:@selector(invoke:)]; }
结果就是出了这个作用域 target 对象释放。通过查阅文档,我在《OC 编程概念》的 Target-Action 一节中,看到苹果有提到这么一句: Control objects do not (and should not) retain their targets.
按照惯例,如果有特殊情况,苹果会特别提醒(比如 NSTimer 的描述中就写到 target The timer maintains a strong reference to this object until it (the timer) is invalidated.
)。所以 UIGestureRecognizer 是不会对 target 强引用。一旦出了作用域,target 对象就释放了。所以,需要使用动态绑定强制持有。
一行代码扩大视图点击区域:
self.button.touchExtendInset = UIEdgeInsetsMake(-10, -10, -10, -10)
实现代码如下:
void Swizzle(Class c, SEL orig, SEL new) { Method origMethod = class_getInstanceMethod(c, orig); Method newMethod = class_getInstanceMethod(c, new); if (class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))){ class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); } else { method_exchangeImplementations(origMethod, newMethod); } } @implementation UIView (ExtendTouchRect) + (void)load { Swizzle(self, @selector(pointInside:withEvent:), @selector(myPointInside:withEvent:)); } - (BOOL)myPointInside:(CGPoint)point withEvent:(UIEvent *)event { if (UIEdgeInsetsEqualToEdgeInsets(self.touchExtendInset, UIEdgeInsetsZero) || self.hidden || ([self isKindOfClass:UIControl.class] && !((UIControl *)self).enabled)) { return [self myPointInside:point withEvent:event]; // original implementation } CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, self.touchExtendInset); hitFrame.size.width = MAX(hitFrame.size.width, 0); // don't allow negative sizes hitFrame.size.height = MAX(hitFrame.size.height, 0); return CGRectContainsPoint(hitFrame, point); } static char touchExtendInsetKey; - (void)setTouchExtendInset:(UIEdgeInsets)touchExtendInset { objc_setAssociatedObject(self, &touchExtendInsetKey, [NSValue valueWithUIEdgeInsets:touchExtendInset], OBJC_ASSOCIATION_RETAIN); } - (UIEdgeInsets)touchExtendInset { return [objc_getAssociatedObject(self, &touchExtendInsetKey) UIEdgeInsetsValue]; } @end
实现思路就是替换 pointInside:withEvent:
或者 hitTest:withEvent:
方法。顺便再复习一下响应链传递机制:当手指触摸屏幕,UIWindow 从最底层开始向上分发事件,每到一个视图,先调用 hitTest:withEvent:
,在 hitTest:withEvent:
里调用 pointInside:withEvent:
判断触摸点是否在当前区域,如果在,遍历它的子视图递归调用 hitTest:withEvent:
。画成二叉树图就是一个反向深度优先遍历,一旦找到第一个最深的包含触摸点的后裔就停止遍历。