您的当前位置:首页正文

扩大按钮UIButton的点击范围

来源:图艺博知识网

首先,我们得继承一个UIButton,然后重写 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 这个方法,苹果的概念是点击区域最好不小于44 point,所以我们根据这个数值计算我们的点击区域。上代码。

自定义按钮 扩大按钮点击范围

```

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

{

CGRectbounds =self.bounds;

CGFloatwidthDelta =44.0- bounds.size.width;

CGFloatheightDelta =44.0- bounds.size.height;

bounds =CGRectInset(bounds, -0.5* widthDelta, -0.5* heightDelta);//注意这里是负数,扩大了之前的bounds的范围

returnCGRectContainsPoint(bounds, point);

}

```

Top