這里分兩種
1.button自身frame 以倍數(shù)擴大
2.button自身frame 以上下左右增加邊距擴大
普遍做法震缭, button增加一個分類
廢話不多說直接上代碼
方法一
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIButton (MGTouchArea)
@property (nonatomic, copy) NSString *clickArea;
@end
#import "UIButton+MGTouchArea.h"
#import <objc/runtime.h>
@interface UIButton()
@end
@implementation UIButton (MGTouchArea)
//重寫
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
[super pointInside:point withEvent:event];
// 獲取bounds 實際大小
CGRect bounds = self.bounds;
if (self.clickArea) {
CGFloat area = [self.clickArea floatValue];
CGFloat widthDelta = MAX(area * bounds.size.width - bounds.size.width, .0);
CGFloat heightDelta = MAX(area * bounds.size.height - bounds.size.height, .0);
//擴大bounds
bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
}
// 點擊的點在新的bounds 中 就會返回YES
return CGRectContainsPoint(bounds, point);
}
- (void)setClickArea:(NSString *)clickArea
{
objc_setAssociatedObject(self, @selector(clickArea), clickArea, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)clickArea
{
return objc_getAssociatedObject(self, @selector(clickArea));
}
@end
用法:
// 這里就是按鈕的實際可點擊區(qū)域大小,可點擊面積等效于按鈕的尺寸乘以該參數(shù)
button.clickArea = @"10";
此處引薦于此篇華文 [https://www.cnblogs.com/CH520/p/12820405.html]
在此表示感謝
方法二
#import <UIKit/UIKit.h>
@interface UIButton (EnlargeTouchArea)
//擴大按鈕點擊范圍
- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left;
@end
#import "UIButton+EnlargeTouchArea.h"
#import <objc/runtime.h>
@implementation UIButton (EnlargeTouchArea)
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
- (void) setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left
{
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGRect) enlargedRect
{
NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
if (topEdge && rightEdge && bottomEdge && leftEdge)
{
return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
self.bounds.origin.y - topEdge.floatValue,
self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
}
else
{
return self.bounds;
}
}
- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
CGRect rect = [self enlargedRect];
if (CGRectEqualToRect(rect, self.bounds))
{
return [super hitTest:point withEvent:event];
}
return CGRectContainsPoint(rect, point) ? self : nil;
}
@end
用法
[button setEnlargeEdgeWithTop:10 right:500 bottom:10 left:50];