扩大按钮UIButton的点击范围

首先,我们得继承一个UIButton,然后重写了按钮中的pointInside方法。
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 这个方法,使得按钮热区不够44×44大小的先自动缩放到44×44,再判断触摸点是否在新的热区内。

- (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);//注意这里是负数,扩大了之前的bounds的范围
    return CGRectContainsPoint(bounds, point);
}

猜你喜欢

转载自blog.csdn.net/ivolcano/article/details/77837943
今日推荐