在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大uibutton的响应范围,下面有两个解决办法:
第一种方法:创建一个类目:uibutton+enlargetoucharea
.h文件
1
2
3
4
5
6
7
8
|
#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文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
//
// 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
|
使用方法:
1
2
3
4
|
uibutton *button = [uibutton new ];
[button setenlargeedge:20];
//或者
[button setenlargeedgewithtop:20 right:20 bottom:20 left:20];
|
第二种:直接创建一个uibutton类,然后复写pointinside方法
使用时继承于此类即可
.h文件
1
2
3
4
5
|
#import <uikit/uikit.h>
@interface biggerclickareabutton : uibutton
@end
|
.m文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#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
|
使用时直接继承创建即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/rglmuselily/archive/2018/08/27/9543075.html