UIButton类别篇

2020-04-10  本文已影响0人  时无寻

扩大Button点击事件

.h文件

#import <UIKit/UIKit.h>
@interface UIButton (XXHitTestInsets)
@property (nonatomic, assign) UIEdgeInsets xx_hitTestInsets;
@end

.m文件

#import "UIButton+XXHitTestInsets.h"
#import <objc/runtime.h>

@implementation UIButton (XXHitTestInsets)

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    CGRect b = self.bounds;
    UIEdgeInsets areaForTouch = self.xx_hitTestInsets;
    CGRect expandRect = CGRectMake(b.origin.x - areaForTouch.left,
                                   b.origin.y - areaForTouch.top,
                                   b.size.width + areaForTouch.left + areaForTouch.right,
                                   b.size.height + areaForTouch.top + areaForTouch.bottom);
    return CGRectContainsPoint(expandRect, point);
}

@end
上一篇 下一篇

猜你喜欢

热点阅读