iOS之UIButton扩大按钮的响应区域
程序员文章站
2023-04-07 10:22:32
在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大UIButton的响应范围,下面有两个解决办法: 第一种方法:创建一个类目:UIButton+EnlargeTouchArea .h文件 .m文件 使用方法: 第二种:直接创建一个UIButton类,然后复写pointInside方法 使 ......
在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大uibutton的响应范围,下面有两个解决办法:
第一种方法:创建一个类目:uibutton+enlargetoucharea
.h文件
#import <uikit/uikit.h> @interface uibutton (enlargetoucharea) - (void)setenlargeedgewithtop:(cgfloat) top right:(cgfloat) right bottom:(cgfloat) bottom left:(cgfloat) left; - (void)setenlargeedge:(cgfloat) size; @end
.m文件
// // uibutton+enlargetoucharea.m // hebeifm // // created by apple on 16/4/27. // copyright © 2016年 apple. all rights reserved. // #import "uibutton+enlargetoucharea.h" #import <objc/runtime.h> @implementation uibutton (enlargetoucharea) static char topnamekey; static char rightnamekey; static char bottomnamekey; static char leftnamekey; - (void)setenlargeedge:(cgfloat) size { objc_setassociatedobject(self, &topnamekey, [nsnumber numberwithfloat:size], objc_association_copy_nonatomic); objc_setassociatedobject(self, &rightnamekey, [nsnumber numberwithfloat:size], objc_association_copy_nonatomic); objc_setassociatedobject(self, &bottomnamekey, [nsnumber numberwithfloat:size], objc_association_copy_nonatomic); objc_setassociatedobject(self, &leftnamekey, [nsnumber numberwithfloat:size], objc_association_copy_nonatomic); } - (void) setenlargeedgewithtop:(cgfloat) top right:(cgfloat) right bottom:(cgfloat) bottom left:(cgfloat) left { objc_setassociatedobject(self, &topnamekey, [nsnumber numberwithfloat:top], objc_association_copy_nonatomic); objc_setassociatedobject(self, &rightnamekey, [nsnumber numberwithfloat:right], objc_association_copy_nonatomic); objc_setassociatedobject(self, &bottomnamekey, [nsnumber numberwithfloat:bottom], objc_association_copy_nonatomic); objc_setassociatedobject(self, &leftnamekey, [nsnumber numberwithfloat:left], objc_association_copy_nonatomic); } - (cgrect) enlargedrect { nsnumber* topedge = objc_getassociatedobject(self, &topnamekey); nsnumber* rightedge = objc_getassociatedobject(self, &rightnamekey); nsnumber* bottomedge = objc_getassociatedobject(self, &bottomnamekey); nsnumber* leftedge = objc_getassociatedobject(self, &leftnamekey); if (topedge && rightedge && bottomedge && leftedge) { return cgrectmake(self.bounds.origin.x - leftedge.floatvalue, self.bounds.origin.y - topedge.floatvalue, self.bounds.size.width + leftedge.floatvalue + rightedge.floatvalue, self.bounds.size.height + topedge.floatvalue + bottomedge.floatvalue); } else { return self.bounds; } } - (uiview*) hittest:(cgpoint) point withevent:(uievent*) event { cgrect rect = [self enlargedrect]; if (cgrectequaltorect(rect, self.bounds)) { return [super hittest:point withevent:event]; } return cgrectcontainspoint(rect, point) ? self : nil; } @end
使用方法:
uibutton *button = [uibutton new]; [button setenlargeedge:20]; //或者 [button setenlargeedgewithtop:20 right:20 bottom:20 left:20];
第二种:直接创建一个uibutton类,然后复写pointinside方法
使用时继承于此类即可
.h文件
#import <uikit/uikit.h> @interface biggerclickareabutton : uibutton @end
.m文件
#import "biggerclickareabutton.h" @implementation biggerclickareabutton - (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); return cgrectcontainspoint(bounds, point); } @end
使用时直接继承创建即可
上一篇: 美国宇航局研发RoboGlove手套,让你拥有超人的握持力
下一篇: 小学时候