iOS开发中怎么扩大按钮的点击范围

方法:
为UIButton增加一个分类,在分类中重写UIButton的pointInside方法,在该方法中改变UIButton的bounds

代码:

  - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{

[super pointInside:point withEvent:event];
//获取bounds 实际大小
CGRect bounds = self.bounds;
if (self.clickArea) {
    CGFloat area = [self.clickArea floatValue];
    CGFloat widthDelta = MAX(area * bounds.size.width - bounds.size.width, .0);
    CGFloat heightDelta = MAX(area * bounds.size.height - bounds.size.height, .0);
    //扩大bounds
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
}
//点击的点在新的bounds 中 就会返回YES
return CGRectContainsPoint(bounds, point);
}

demo地址:https://github.com/yangguanghei/-

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。