我无法触摸的 UIButton

发布于 2024-12-27 07:17:47 字数 1253 浏览 1 评论 0原文

我在 pickerView 上方创建了一个按钮来关闭它。 我想我做到了 这里是启动 pickerView 及其关闭按钮的代码。

    UIPickerView *tempPickerView = [[[UIPickerView alloc] initWithFrame:
        CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
    tempPickerView.showsSelectionIndicator = YES;
    tempPickerView.delegate = self;
    tempPickerView.dataSource = self;
    UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, -32, 52, 32)];
    [pickerButton setBackgroundImage:[UIImage imageNamed:@"week.png"] 
         forState:UIControlStateNormal];
    [pickerButton addTarget:self action:@selector(hidePicker) 
         forControlEvents:UIControlEventTouchUpInside];
    [tempPickerView addSubview:pickerButton];
    [tempPickerView bringSubviewToFront:pickerButton];
    [pickerButton release];

    [self.view addSubview:tempPickerView];
    return tempPickerView;

它看起来像这样:

在此处输入图像描述

该按钮位于 pickerView 的左上角,忘记“周”,这只是一个临时名称。

问题是,我无法触摸这个按钮,每次我尝试触摸它时,我只是直接触摸它后面的东西,例如talbeViewCell。

是因为我在 pickerView 框架之外添加了按钮吗?

我的意思是按钮原点的坐标是(0,-32),

这样它就会显示在pickerView上方,坐标有问题吗?

谁能告诉如何解决这个问题?

非常非常感谢!!!

I made a button above a pickerView to dismiss it.
and I think I made it,
here the the code that initiates the pickerView as well as its dismiss button.

    UIPickerView *tempPickerView = [[[UIPickerView alloc] initWithFrame:
        CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
    tempPickerView.showsSelectionIndicator = YES;
    tempPickerView.delegate = self;
    tempPickerView.dataSource = self;
    UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, -32, 52, 32)];
    [pickerButton setBackgroundImage:[UIImage imageNamed:@"week.png"] 
         forState:UIControlStateNormal];
    [pickerButton addTarget:self action:@selector(hidePicker) 
         forControlEvents:UIControlEventTouchUpInside];
    [tempPickerView addSubview:pickerButton];
    [tempPickerView bringSubviewToFront:pickerButton];
    [pickerButton release];

    [self.view addSubview:tempPickerView];
    return tempPickerView;

and it looks like this:

enter image description here

that button is at the upper left cornor of the pickerView, forget about the "week", it is just a temporary name.

Problem is, I can't touch this button, everytime I tried to touch it, I just directly touched things behind it such as the talbeViewCell.

Is is because I added the button outside of the pickerView's frame?

I mean the coordinate of the button's origin is (0, -32),

so that it would be displayed above the pickerView, is there any problems with the coordinate?

Can anyone tell how to fix this problem?

Thanks a lot and a lot!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2025-01-03 07:17:47

问题是您已将按钮放置在其父视图的边界之外。 iOS 现在会这样做(经过简化,不是 100% 正确,但你明白了):

  • 它注意到发生了触摸。它计算该触摸的坐标。
  • 然后它尝试找出哪个视图被触及。为此,它会调用应用的 UIWindow 对象上的 hitTest:withEvent: 方法。
  • 反过来,它会在其每个子级上调用 hitTest:withEvent: ,并且如果需要,这些子级也会在其子级上调用 hitTest:withEvent:
  • 当调用视图的 hitTest:withEvent: 时,它首先查看坐标是否在其自己的边界内 (IIRC)。如果不是,则返回nil。否则,它会对其子级调用 hitTest:withEvent:

hitTest:withEvent: 文档 明确指出:

位于接收器边界之外的点永远不会被报告为命中,即使它们实际上位于接收器的子视图之一内。

现在的问题是您的按钮位于其父视图之外。因此,hitTest:withEvent: 确定触摸坐标不在选取器视图的边界内,并且从不询问其子级(按钮)是否可能被击中。

您可以对选择器进行子类化并重写 hitTest:withEvent: 方法来“修复”此问题,但真正的解决方法是移动按钮,使其成为选择器的同级按钮,而不是子按钮。

The problem is that you have placed the button outside the bounds of its parent view. iOS now does this (simplified, not 100% correct but you get the deal):

  • It notices a touch occurred. It calculates the coordinates of that touch.
  • It then tries to find out which view was touched. To do so, it calls the hitTest:withEvent: method on the UIWindow object of your app.
  • That in turn calls hitTest:withEvent: on each of its children, and these each call hitTest:withEvent: on their children if necessary.
  • When a view's hitTest:withEvent: is called, it first looks whether the coordinate is within its own bounds (IIRC). If it isn't, it returns nil. Otherwise it calls hitTest:withEvent: on its children.

The hitTest:withEvent: documentation explicitly says:

Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews.

The problem now is that your button is outside of its parent view. So hitTest:withEvent: determines that the touch coordinate is not inside the bounds of the picker view and never bothers to ask its child (the button) whether it might be hit.

You could subclass the picker and override the hitTest:withEvent: method to "fix" this, but the real fix is to move the button so its a sibling of the picker, not a child.

触ぅ动初心 2025-01-03 07:17:47

我认为这可能是由UIPickerView的命中测试方法引起的。如果触摸超出其范围,它可能会从 pointInside:withEvent: 返回 NO。头文件(UIView.h)注释说

如果点在边界内,则默认返回 YES

由于您将按钮放置在 UIPickerView 的边界之外,这很可能就是原因。

作为解决方法,您可以将按钮插入选择器的超级视图中。

I think this may be caused by UIPickerView's hit-testing method. It may return NO from pointInside:withEvent: if the touch is outside its bounds. The header file (UIView.h) comment says

default returns YES if point is in bounds

Since you place the button outside the bounds of UIPickerView, that may well be the reason.

As a workaround, you can insert the button into the picker's superview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文