iphone - 防止禁用的 UIButton 传播触摸事件

标签 iphone objective-c cocoa-touch event-handling uibutton

我的应用程序有两个重叠的 UIButton。顶部按钮有时可能会被禁用。但是,在这种情况下,它接收到的任何触摸事件似乎都传递到底层 View ,在我的例子中是另一个按钮。

我需要的是顶部按钮拦截所有触摸并防止它们到达底部按钮,即使在禁用状态下也是如此(即使在禁用状态下调用指定的操作我也会很高兴)。

到目前为止我已经尝试过:

[topButton setUserInteractionEnabled:YES];

[topButton setExclusiveTouch:YES];

虽然后一种情况可能是不可取的,因为如果它是第一个点击的 View ,我仍然需要底部按钮响应事件。无论哪种方式,它们都不起作用。

最佳答案

我在禁用按钮的 super View 中添加了以下方法:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (!self.watchButton.enabled &&
        [self.watchButton pointInside:[self convertPoint:point toView:self.watchButton]
                            withEvent:nil]) {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}

这适用于 UITableView 单元格。

关于iphone - 防止禁用的 UIButton 传播触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2055372/

相关文章:

iphone - 使用 CoreAnimation 在 iOS 中实现流畅的动画

iphone - 应用程序进入前台时崩溃-因为委托(delegate)= self ?

objective-c - NSTask 在 find 中调用 perl 和管道不起作用

objective-c - NSButton 以编程方式列出或解除现有绑定(bind)

iphone - 切换 View Controller 时动画是否自动从图层中移除?

iphone - 如何旋转 UIView?

ios - 将应用程序上传到 App Store 需要 7 到 8 天。是否有任何解决方案可以将其上传到 App Store 的最短天数?

objective-c - 将问题 Objective-C 转换为 Swift

使用 NSTimer 通过单元测试后,iOS 逻辑单元测试目标崩溃

iphone - reloadData 没有被调用