忽略触摸事件,让它下面的视图处理触摸吗? [英] Ignore touch event, and let the view below it handle the touch?

查看:77
本文介绍了忽略触摸事件,让它下面的视图处理触摸吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户将触摸的可拖动视图,但是它的某些矩形将没有图像(字母0).

I have a draggable view that a user will touch, but some rectangles of it will have no image (alpha 0).

当用户单击透明区域时(我可以构建不带alpha信息的透明区域),我希望透明区域下方的视图(相同类)可以检测触摸.

When a user clicks the transparent region (I am able to construct the transparent region without the alpha info), I want the view (same class) below the transparent region to detect the touch.

我的策略是在用户触摸透明区域时让视图忽略触摸,并希望其下方的视图能够自动捕获触摸事件.但是我不确定这是否行得通.(进行设置以进行测试将需要一些时间)

My strategy is to let the view ignore the touch when user touches the transparent area and hope the view below it will automatically catch the touch event. But I'm not sure if this will work. (setting things up to test this will take some time)

我应该采用其他方法还是上述策略行得通?

Should I take a different approach or the above strategy would work?

谢谢.

推荐答案

尝试覆盖超级视图中的hitTest:withEvent:方法.您可以使hitTest:withEvent:返回要处理给定事件的视图.

Try overwriting the method hitTest:withEvent: in the superview. You can make hitTest:withEvent: return the view you want to handle a given event.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    for (UIView *v in self.subviews){
        CGPoint pointInB = [v convertPoint:point fromView:self];
        if ([v someConditionYouMayWantToTestFor]){
            return v;
        }
    }
    return nil;
}

someConditionYouMayWantToTestFor方法是测试子视图是否捕获事件的地方.

The method someConditionYouMayWantToTestFor is where you test if you want the subview to capture the event or not.

这篇关于忽略触摸事件,让它下面的视图处理触摸吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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