我的应用程序有两个重叠的 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/