禁用触摸UIView背景,以便可以单击较低视图上的按钮 [英] Disable touches on UIView background so that buttons on lower views are clickable

查看:94
本文介绍了禁用触摸UIView背景,以便可以单击较低视图上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,这是我的视图层次结构(如果这很难读,我会道歉......我是新来的,所以发布的建议很乐意接受)

Basically, here is my view hierarchy (and I appologize if this is hard to read... I'm new here so posting suggestions happily accepted)

- AppControls.xib

-------(UIView)ControlsView

---------- -------(UIView)TopBar

----------------- -------------- btn1 ,btn2,btn3

----------------- UIView)BottomBar

------------ ----- -------------- slider1 btn1,btn2

- PageContent.xib

-------- ---------(UIView)ContentView

----------------- ------------- -btn1,btn2,btn3

----------------- --------------(UIImageView)FullPageImage

--AppControls.xib
-------(UIView)ControlsView
----------------- (UIView)TopBar
----------------- -------------- btn1, btn2, btn3
----------------- UIView)BottomBar
----------------- --------------slider1 btn1, btn2
--PageContent.xib
----------------- (UIView)ContentView
----------------- --------------btn1, btn2, btn3
----------------- --------------(UIImageView)FullPageImage

我的情况是,我想隐藏并在点击PageContent上的任何位置时显示控件,而不是按钮并拥有控件显示,很像iPhone视频播放器。但是,当显示控件时,我仍然希望能够单击PageContent上的按钮。

My situation is that I want to hide and show the controls when tapping anywhere on the PageContent thats not a button and have the controls show, much like the iPhone Video Player. However, when the controls are shown I still want to be able to click the buttons on the PageContent.

除了最后一点,我完成了所有这些工作。当控件显示控件的背景时,接收触摸事件而不是下面的视图。在ControlsView上关闭用户交互会将其所有子节点关闭。

I have all of this working, except for the last bit. When the controls are showing the background of the controls receives the touch events instead of the view below. And turning off user interaction on the ControlsView turns it off on all its children.

我已尝试在我的ControlsView子类上覆盖HitTest,如下所示,我在类似的帖子中找到:

I have tried overriding HitTest on my ControlsView subclass as follows which I found in a similar post:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *hitView = nil;
NSArray *subviews = [self subviews];
int subviewCount = [subviews count];
for (int subviewIndex = 0; !hitView && subviewIndex < subviewCount; subviewIndex++){
hitView = [[subviews objectAtIndex:subviewIndex] hitTest:point withEvent:event];
}   
return hitView;
}

然而,此时我的滑块不起作用,大多数也没有其他按钮,实际上,事情开始变得怪异。

However, at this point my slider doesn't work, nor do most of the other buttons, and really, things just start getting weird.

所以我的问题很简单:如何让视图的所有子视图都有触摸事件,而超级视图的背景是不可点击的,下面视图上的按钮可以接收触摸事件。

So my question is in short: How do I let all the subviews of a view have touch events, while the super view's background is unclickable, and the buttons on views below can receive touch events.

谢谢!

推荐答案

你很接近。不要覆盖 -hitTest:withEvent:。到调用时,事件调度程序已经确定您的层次结构的子树拥有该事件,并且不会查找其他地方。相反,覆盖 -pointInside:withEvent:,它在事件处理管道中先前调用。这就是系统如何询问嘿视图,你的层次结构中的任何人在此时对事件做出响应吗?。如果您拒绝,则事件处理将在可见堆栈中继续在您下方。

You're close. Don't override -hitTest:withEvent:. By the time that is called, the event dispatcher has already decided that your subtree of the hierarchy owns the event and won't look elsewhere. Instead, override -pointInside:withEvent:, which is called earlier in the event processing pipeline. It's how the system asks "hey view, does ANYONE in your hierarchy respond to an event at this point?". If you say NO, event processing continues below you in the visible stack.

文档,默认实现只检查点是否完全在视图的范围内。

Per the documentation, the default implementation just checks whether the point is in the bounds of the view at all.

当您的任何子视图位于该坐标时,您的策略是说是,但是当触摸将触及背景时说不。

Your strategy is to say "yes" when any of your subviews is at that coordinate, but say "no" when the touch would be hitting the background.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    for (UIView * view in [self subviews]) {
        if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
            return YES;
        }
    }
    return NO;
}

这篇关于禁用触摸UIView背景,以便可以单击较低视图上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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