iOS runtime扩大UIButton点击范围

    xiaoxiao2021-03-25  138

             我们知道C代码一般经过预处理、编译、链接、运行然后在平台上跑起来,Objective-C是基于C加入了面向对象和消息转发的动态语言,所以除了需要一个编译器外,还需runtime系统来动态创建类和对象。

           iOS Category可以给一个现有类添加属性,当然不是简单的添加,需要用runtime机制。但是我们是不是总是看到类别中只能添加方法,不能添加实例变量,那这句话不是有问题了么?当然没问题,本来类别中只能添加方法,不能添加实例变量,在这里,我们要明白属性和实例变量了,以前声明属性必须声明与之对应的实例变量,这是早期的概念,苹果把编译器宠GCC转为LLVM,从此不再需要为属性声明实例变量了,如果LLVM发现一个没有匹配实例变量的属性,它将自动创建一个以下划线开头的实例变量。

         有时候一个button显示得很小,点击时候很难点中,这时可以扩大button的点击范围。建一个UIButton的分类,添加一个方法,可以传入一个float类型的size,使button四边扩大相同的大小,也可以针对button四边传入不同的size,扩大需要的size。

    #import <UIKit/UIKit.h> #import <objc/runtime.h> @interface UIButton (ButtonExpand) - (void)expandSize:(CGFloat)size; @end

    #import "UIButton+ButtonExpand.h" @implementation UIButton (ButtonExpand) static char expandSizeKey; - (void)expandSize:(CGFloat)size { //OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) //OBJC_EXPORT 打包lib时,用来说明该函数是暴露给外界调用的。 //id object 表示关联者,是一个对象 //id value 表示被关联者,可以理解这个value最后是关联到object上的 //const void *key 被关联者也许有很多个,所以通过key可以找到指定的那个被关联者 objc_setAssociatedObject(self, &expandSizeKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC); } //获取设置的扩大size,来扩大button的rect //当前只是设置了一个扩大的size,当然也可以设置4个扩大的size,上下左右,具体扩大多少对应button的四个边传入对应的size - (CGRect)expandRect { NSNumber *expandSize = objc_getAssociatedObject(self, &expandSizeKey); if (expandSize) { return CGRectMake(self.bounds.origin.x - expandSize.floatValue, self.bounds.origin.y - expandSize.floatValue, self.bounds.size.width + expandSize.floatValue + expandSize.floatValue, self.bounds.size.height + expandSize.floatValue + expandSize.floatValue); } else { return self.bounds; } } //响应用户的点击事件 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { CGRect buttonRect = [self expandRect]; if (CGRectEqualToRect(buttonRect, self.bounds)) { return [super pointInside:point withEvent:event]; } return CGRectContainsPoint(buttonRect, point) ? YES : NO; }

    转载请注明原文地址: https://ju.6miu.com/read-852.html

    最新回复(0)