Ios UIButtonTypeInfoDark触摸区
我在视图中添加了一个类型为Ios UIButtonTypeInfoDark触摸区,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我在视图中添加了一个类型为UIButtonTypeInfoDark的UIButton,它的触摸区域非常大。我知道苹果公司推荐44px,但在这种情况下,它要大一点。我为视图设置了浅灰色背景,以查看44px的结束区域在哪里,并且我能够触摸浅灰色视图区域之外的区域,并且仍然收到infoTapped:事件 有人能澄清原因吗?谢谢 _infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark]; [_infoButton setShowsTouch
UIButtonTypeInfoDark
的UIButton
,它的触摸区域非常大。我知道苹果公司推荐44px,但在这种情况下,它要大一点。我为视图设置了浅灰色背景,以查看44px的结束区域在哪里,并且我能够触摸浅灰色视图区域之外的区域,并且仍然收到infoTapped:
事件
有人能澄清原因吗?谢谢
_infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
[_infoButton setShowsTouchWhenHighlighted:NO];
[_infoButton setFrame:CGRectMake(frame.size.width-44, 0, 44, 25)];
[_infoButton setBackgroundColor:[UIColor lightGrayColor]];
[_infoButton addTarget:self action:@selector(infoTapped:) forControlEvents:UIControlEventTouchUpInside];
要解决这个问题,很可能需要子类化。您可以在
UIButton
子类中重写并返回NO
,除非严格在按钮的范围内:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
return CGRectContainsPoint(self.bounds, point);
}
苹果控件有时会返回
YES
到其边界之外的查询,以使控件更易于用手指触摸。有一种更简单的版本,可以检查用户是触摸信息按钮边界的内部还是外部。只需检查touchInside的状态
if ([control isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton*)control;
if (btn.buttonType == UIButtonTypeInfoDark && btn.touchInside == YES) {
// User pressed button inside bounds...
} else if (btn.buttonType == UIButtonTypeInfoDark && btn.touchInside == NO) {
// User pressed button outside bounds...
}
}
尝试设置中心而不是框架。然后将其精确帧记录在infoTapped中。我很想知道它的精确值。我使用了下面的代码,得到了下面的日志(x,y,width,height)。它必须将视图坐标外的触摸区域重置为44px宽。75.0000000.00000018.00000018.000000[_infobuttonsetframe:CGRectMake(frame.size.width-25,0,18,18)];[_infobuttonsetcenter:CGPointMake(frame.size.width-25+9,9)];我建议你检查一下这个问题。这不是一个解决办法,而是我这边的一点帮助。