美文网首页iOS常用
iOS开发中怎么扩大按钮的点击范围

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

作者: 梁森的简书 | 来源:发表于2020-06-20 19:07 被阅读0次

方法:
为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/-

相关文章

网友评论

    本文标题:iOS开发中怎么扩大按钮的点击范围

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