当userinteractionenabled为NO时触摸

时间:2014-01-21 03:59:19

标签: ios uigesturerecognizer

似乎有一个answer,但是当我尝试提供的方法时,它就不起作用了!

@interface MyView : UIView

@end

@implementation MyView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    NSLog(@"hitView:%@", hitView);
    if (hitView == self) {
        return nil;
    }
    return hitView;
}

- (void)testUserInteraction
{
    UIViewController *vc = [[UIViewController alloc] init];
    self.window.rootViewController = vc;

    MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    myView.userInteractionEnabled = NO;
    [vc.view addSubview:myView];

    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    subView.backgroundColor = [UIColor orangeColor];
    [myView addSubview:subView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
    [subView addGestureRecognizer:tap];
}

myView.userInteractionEnabled = YES;一切正常时。但是当myView.userInteractionEnabled = NO;点击屏幕时,只需输出hitView:(null)

这种方法不再有效,还是我错过了什么?

2 个答案:

答案 0 :(得分:1)

是的,因为您禁用了用户互动 当你调用super-hittest时,super-hittest的返回值取决于userInteraction属性。

如果仅启用它,则返回自身或某个子视图 如果未启用,它将始终返回nil。

答案 1 :(得分:0)

如果我理解正确,您想知道自定义UIView内部何时发生触摸,同时禁用视图上的用户交互。如果是这样,请将以下内容抛入 - (UIView *)hitTest:(CGPoint)点withEvent:(UIEvent *)事件方法。此条件应仅读取自定义视图中的触摸事件

if ([self pointInside:point withEvent:event]) {
    NSLog(@"TOUCH IS INSIDE THE VIEW");
}