美文网首页
扩大UIButton可点击区域方法

扩大UIButton可点击区域方法

作者: JasonEVA | 来源:发表于2016-09-10 15:28 被阅读61次

写一个分类,重写UIButton的- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event方法,对响应热区进行放大

上代码

#import "UIButton+EX.h"

@implementation UIButton (EX)
- (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

相关文章

网友评论

      本文标题:扩大UIButton可点击区域方法

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