创建分类,使用objc运行时交换 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 方法的实现,在自己的实现逻辑中进行判断,添加 extendedHitArea属性 是你要扩展的可以点击的区域,在pointInside方法里判断点击的坐标是否在可响应点击区域,如果在返回YES,否则返回NO 。
//
// UIButton+ExtendArea.h
// ExtendClickArea
//
// Created by admin on 2020/11/2.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIButton (ExtendArea)
@property(nonatomic,assign) CGRect extendedHitArea;
@end
NS_ASSUME_NONNULL_END
//
// UIButton+ExtendArea.m
// ExtendClickArea
//
// Created by admin on 2020/11/2.
//
#import "UIButton+ExtendArea.h"
#import <objc/runtime.h>
@implementation UIButton (ExtendArea)
// extendHitArea 是暴露出来的一个属性 。
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method originalM = class_getInstanceMethod([self class], @selector(pointInside:withEvent:));
Method swzM = class_getInstanceMethod([self class], @selector(swz_pointInside:withEvent:));
BOOL addMethod = class_addMethod([self class], @selector(pointInside:withEvent:), method_getImplementation(swzM), method_getTypeEncoding(swzM));
if (addMethod) {
class_replaceMethod([self class], @selector(swz_pointInside:withEvent:), method_getImplementation(originalM), method_getTypeEncoding(originalM));
} else {
method_exchangeImplementations(originalM, swzM);
}
});
}
- (void)setExtendedHitArea:(CGRect)extendedHitArea {
objc_setAssociatedObject(self, @selector(extendedHitArea), @(extendedHitArea), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGRect)extendedHitArea {
CGRect rect = [objc_getAssociatedObject(self, @selector(extendedHitArea)) CGRectValue];
if (CGRectEqualToRect(rect, CGRectZero)) {
return self.bounds;
}
return rect;
}
/*!
@method 可以响应点击事件的rect
*/
- (CGRect)responseHitArea {
CGRect extendedHitArea = [objc_getAssociatedObject(self, @selector(extendedHitArea)) CGRectValue];
if (!CGRectEqualToRect(extendedHitArea, CGRectZero)) {
return CGRectMake(
self.bounds.origin.x-extendedHitArea.origin.x,
self.bounds.origin.y-extendedHitArea.origin.y,
self.bounds.size.width+extendedHitArea.origin.x+extendedHitArea.size.width,
self.bounds.size.height+extendedHitArea.origin.y+extendedHitArea.size.height);
} else {
return self.bounds;
}
}
- (BOOL)swz_pointInside:(CGPoint)point withEvent:(UIEvent *)event {
BOOL result = [self swz_pointInside:point withEvent:event];
CGRect responseHitArea = [self responseHitArea];
BOOL ret = ( result
||CGRectEqualToRect(responseHitArea, CGRectZero)
||!self.isUserInteractionEnabled
||self.isHidden);
if (ret) return result;
return CGRectContainsPoint(responseHitArea, point);
}
@end
使用方法
//
// ViewController.m
// ExtendClickArea
//
// Created by admin on 2020/11/2.
//
#import "ViewController.h"
#import "UIButton+ExtendArea.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *clickBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.clickBtn.extendedHitArea = CGRectMake(0, 0, 200, 100);
}
- (IBAction)clickBtn:(id)sender {
NSLog(@"点击了");
}
@end
参考资料
iOS扩大UIButton
网友评论