本文介绍了如何在最顶层的uiview中忽略触摸事件,当它是清楚和不同的uiview可以处理它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清楚的UIView,它有手势识别器附加到它。



这个清晰的uiview覆盖整个超级视图,允许手势从任何地方被调用



在这个清晰的UIView之下,有不同的组件,如表格,按钮,collectionview等。



我想要的是 - 如果一个视图在清晰uiview下可以处理触摸事件(或任何类型的手势) - 清除视图应忽略该事件 - 事件将传递到可处理它的基础视图。



我尝试了



- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

但我不知道如何确保底层视图可以处理它。

解决方案

我使用了一些建议,并使用以下解决方案:



我将手势识别器添加到最底层的超级视图大多数)



然后在该类中取消

   - *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{
UIView * v = [super hitTest:point withEvent:event];

//如果v是nil,则touch不在此视图或其子视图中
if(v == nil)
{
return nil;
}

//在任何情况下如果topview被隐藏而不返回默认值
if(self.myTopView.hidden)
{
return v;
}

//如果视图没有隐藏,但触摸返回了一个控件 - 比我们可以传递控件
if([v isKindOfClass:[UIControl class]])
{
return v;
}

//决定什么阈值是触摸

CGFloat threshHold = 40;

//如果触摸不在控件上,但可以发起手势比该视图应该得到触摸
if(v.gestureRecognizers)
{
threshHold = 30;
// return v;
}

//检查阈值是否应该更大
if([self someCondition])
{
threshHold = 100;
}

//根据其位置的阈值 - 这是动态部分
if(point.y>(self.myTopView.frame.origin.y - threshold) )
{
return self.handleBarView;
}
return v;
}


I have a clear UIView which has gesture recognizers attached to it.

This clear uiview covers the entire super view to allow for the gestures to be invoked from anywhere on it.

Under this clear UIView sit different components such as tables,buttons,collectionview etc.

The clear UIView has no idea what is under it any time.

What I want - if a view which is under the clear uiview can handle a touch event (or any type of gesture) - the clear view should disregard that event - and the event will pass through to the underlying view which could handle it.

I tried

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

but I don't know how to make sure the underlying view can handle it.

解决方案

I used some of these suggestions and used the following solution:

I added the gesture recognizer to the bottom most superview in the heirarchy (and not the top most)

Then in that class over rid

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *v = [super hitTest:point withEvent:event];

    // if v is nil then touch wasn't in this view or its subviews
    if (v == nil)
    {
        return nil;
    }

    // in any case if the topview was hidden than return the default value
    if (self.myTopView.hidden)
    {
        return v;
    }

    // if the view isn't hidden but the touch returned a control - than we can pass the touch to the control
    if ([v isKindOfClass:[UIControl class]])
    {
        return v;
    }

    // decide on what threshold to decide is a touch

    CGFloat threshHold = 40;

    // if the touch wasn't on a control but could initiate a gesture than that view should get the touch
    if (v.gestureRecognizers)
    {
        threshHold = 30;
//        return v;
    }

// check if the threshold should be bigger
    if ([self someCondition])
    {
        threshHold = 100;
    }

// threshold according to its position - this is the dynamic part
    if (point.y > (self.myTopView.frame.origin.y - threshold))
    {
        return self.handleBarView;
    }
    return v;
}

这篇关于如何在最顶层的uiview中忽略触摸事件,当它是清楚和不同的uiview可以处理它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:57