防止禁用的UIButton传播触摸事件 [英] Prevent disabled UIButton from propagating touch events

查看:106
本文介绍了防止禁用的UIButton传播触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有两个重叠的UIButton。顶部按钮有时可能被禁用。但是,在这种情况下,接收到的任何触摸事件似乎都传递到了基础视图,在我的情况下是另一个按钮。

My application has two overlapping UIButtons. The top button may be disabled at times. However, in this case any touch events it receives seem to be passed down to the underlying view, which in my case is the other button.

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

What I need is the top button intercepting all touches and preventing them from reaching the bottom button, even in disabled state (I would be happy even if it's designated action is invoked in the disabled state).

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

So far I've tried:

[topButton setUserInteractionEnabled:YES];

[topButton setExclusiveTouch:YES];

尽管后一种情况可能是不可取的,因为如果这是第一种情况,我仍然需要底部按钮来响应事件查看已点击。不管哪种方法,它们都不起作用。

though the later case is probably undesirable since I still need the bottom button responding to events if it's the first view clicked. Either way none of them work.

推荐答案

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

I added the following method to superview of the disabled button:

- (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单元格很好。

This works well with UITableView cells.

这篇关于防止禁用的UIButton传播触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆