iOS擴(kuò)大按鈕響應(yīng)范圍

原理:重寫view的下面任一方法

//尋找合適的view
 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//判斷點(diǎn)是否在視圖上
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 

法一

要利用runTime創(chuàng)建類別私有屬性

附上view的拓展類代碼:

@interface UIButton (EnlargeTouchArea)

- (void)enlargeTouchEdge:(UIEdgeInsets)edge;

@end
#import "UIButton+EnlargeTouchArea.h"
#import <objc/runtime.h>

@implementation UIButton (EnlargeTouchArea)


static char edgeKey;

- (NSString *)enlargeEdge {
    return objc_getAssociatedObject(self, &edgeKey);
}

- (void)enlargeTouchEdge:(UIEdgeInsets)edge {
    NSString *edgeStr = NSStringFromUIEdgeInsets(edge);
    objc_setAssociatedObject(self, &edgeKey, edgeStr, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIEdgeInsets changeInsets = UIEdgeInsetsFromString([self enlargeEdge]);
    CGFloat x = self.bounds.origin.x + changeInsets.left;
    CGFloat y = self.bounds.origin.y + changeInsets.top;
    CGFloat width = self.bounds.size.width - changeInsets.left - changeInsets.right;
    CGFloat height = self.bounds.size.height - changeInsets.top - changeInsets.bottom;
    CGRect rect = CGRectMake(x, y, width, height);
    
    if (CGRectEqualToRect(rect, self.bounds)) {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}

//- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//    UIEdgeInsets changeInsets = UIEdgeInsetsFromString([self enlargeEdge]);
//    if (changeInsets.top != 0 || changeInsets.left != 0 || changeInsets.bottom != 0 || changeInsets.right !=0) {
//        CGRect myBounds = self.bounds;
//        myBounds.origin.x = myBounds.origin.x + changeInsets.left;
//        myBounds.origin.y = myBounds.origin.y + changeInsets.top;
//        myBounds.size.width = myBounds.size.width - changeInsets.left - changeInsets.right;
//        myBounds.size.height = myBounds.size.height - changeInsets.top - changeInsets.bottom;
//        return CGRectContainsPoint(myBounds, point);
//    }else {
//        return CGRectContainsPoint(self.bounds, point);
//    }
//}

@end

使用

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
   
   UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
   btn.frame = CGRectMake(150, 200, 200, 200);
   btn.backgroundColor = [UIColor redColor];
   [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:btn];
//...
   [btn enlargeTouchEdge:UIEdgeInsetsMake(-50, -50, -50, -50)];
}

- (void)btnAction {
   NSLog(@"tap");
}

法二

直接創(chuàng)建一個(gè)UIButton類,然后復(fù)寫pointInside\hitTest方法,使用時(shí)繼承于此類即可
附上UIButton類代碼:

@interface EnlargeTouchAreaButton : UIButton
@property (nonatomic, assign) UIEdgeInsets changeInsets;

@end

#import "EnlargeTouchAreaButton.h"

@implementation EnlargeTouchAreaButton

- (void)setChangeInsets:(UIEdgeInsets)changeInsets {
    _changeInsets = changeInsets;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    CGFloat x = self.bounds.origin.x + self.changeInsets.left;
    CGFloat y = self.bounds.origin.y + self.changeInsets.top;
    CGFloat width = self.bounds.size.width - self.changeInsets.left - self.changeInsets.right;
    CGFloat height = self.bounds.size.height - self.changeInsets.top - self.changeInsets.bottom;
    CGRect rect = CGRectMake(x, y, width, height);
    
    if (CGRectEqualToRect(rect, self.bounds)) {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}

//- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//    if (self.changeInsets.top != 0 || self.changeInsets.left != 0 || self.changeInsets.bottom != 0 || self.changeInsets.right !=0) {
//        CGRect myBounds = self.bounds;
//        myBounds.origin.x = myBounds.origin.x + self.changeInsets.left;
//        myBounds.origin.y = myBounds.origin.y + self.changeInsets.top;
//        myBounds.size.width = myBounds.size.width - self.changeInsets.left - self.changeInsets.right;
//        myBounds.size.height = myBounds.size.height - self.changeInsets.top - self.changeInsets.bottom;
//        return CGRectContainsPoint(myBounds, point);
//    }else {
//        return CGRectContainsPoint(self.bounds, point);
//    }
//}

@end

使用

EnlargeTouchAreaButton *btn2 = [EnlargeTouchAreaButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(150, 200, 200, 200);
    btn2.backgroundColor = [UIColor redColor];
    [btn2 addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    btn2.changeInsets = UIEdgeInsetsMake(-50, -50, -50, -50);

參考(https://blog.csdn.net/u013282507/article/details/51259598

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容