欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS UIButton扩大按钮响应区域的解决方法

程序员文章站 2022-07-04 20:17:48
在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大uibutton的响应范围,下面有两个解决办法: 第一种方法:创建一个类目:uibutton+enlarge...

在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大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

使用时直接继承创建即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。