修改UIButton的可点击区域

1,303 阅读1分钟

项目中有很多按钮由于创建时根据图片大小来创建,导致可点击区域过小,不容易触发点击时间,扩大按钮区域的话,按钮图片位置又要单独控制,着实不方便,现在可以按钮保持原来大小,单纯的扩大点击响应区域:

@interface UIButton(PointRect)
@end

@implementation UIButton(PointRect)
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event{
    CGRect bounds = self.bounds;
    //若原来点击区域小于44x44,则放大点击区域,否则保持原大小不变
    CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(44.0 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}
@end

这样,原来的代码可以不用做任何修改,点击区域却比原来大的多。 Bigger than bigger 哈哈