ios 扩大button的点击区域

https://www.jianshu.com/p/7017a514d94e

当UI设计图上的给出按钮尺寸较小,我们将对应的资源文件放入UIButton中,在真机调试中会发现难以点到按钮。
这时候我们可以创建一个类继承UIButton,重写pointInside方法,使得按钮事件响应不够我们设置的最小区域的自动扩大到我们的设置的最小区域。

.h定义我们设置的最小响应区域大小
/**
 *  事件响应最小区域大小(小于此区域则放大,否则保持原大小不变,不赋值保持原大小不变)
 */
@property (nonatomic, assign) CGSize eventFrame;
.m重写pointInside方法
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event{
    
CGRect bounds = self.bounds;
CGFloat widthExtra = MAX(self.eventFrame.width - bounds.size.width, 0);
CGFloat heightExtra = MAX(self.eventFrame.height - bounds.size.height, 0);
bounds = CGRectInset(bounds, -0.5 * widthExtra, -0.5 * heightExtra);
return CGRectContainsPoint(bounds, point);
}


作者:fulen
链接:https://www.jianshu.com/p/7017a514d94e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/itlover2013/p/13575009.html