美文网首页
UIButton类别篇

UIButton类别篇

作者: 时无寻 | 来源:发表于2020-04-10 23:08 被阅读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

相关文章

网友评论

      本文标题:UIButton类别篇

      本文链接:https://www.haomeiwen.com/subject/axgjmhtx.html