iOS常用

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

2020-06-20  本文已影响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/-

上一篇 下一篇

猜你喜欢

热点阅读