ios - 当 userinteractionenabled 为 NO 时获取触摸

标签 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)

那么是这个方法不再有效了,还是我遗漏了什么?

最佳答案

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

如果它被启用,那么它只会返回它自己或一些 subview 。
如果未启用,它将始终返回 nil。

关于ios - 当 userinteractionenabled 为 NO 时获取触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21248901/

相关文章:

ios - 将 UIGestureRecognizer 添加到自定义 drawRect 或 UIBezierPath

ios - 为什么 touchesbegan : never gets called on a UIView after UIPinchGestureRecognizer is used?

iphone - 如何检测用户在iphone sdk中绘制矩形、椭圆形或其他几何图形?

objective-c - 在objective-C中以编程方式为tableView创建导航栏

ios - Swift 出错,需要帮助 : property 'self.circle1' not initialized at super. 初始化调用

ios - 无法从推送通知解析 gcm.notification.data

ios - Swift didSet 已调用但未更新 UILabel - iOS 属性观察器

ios - 图片不能包含 alpha channel 或透明胶片

ios - UIScrollView subview 中的 UILabels 上的 UITapGestureRecognizer 不起作用

ios - 创建自定义 "tickle"UIGestureRecognizer 的最佳方法是什么?