视图的点击手势是否适用于视图内的所有内容



我想在触摸视图中的任意位置时隐藏一些内容。我有一个视图和一些标签,视图作为子视图,如果我为主视图添加点击手势,所有子视图都会得到响应?或者我必须为每个子视图单独添加点击手势?

您可以在

UIView中添加touchesBegan:withEvents:。每次触摸UIView内的元素时,您都可以从触摸中获取所选元素并将其隐藏:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [[event allTouches] anyObject];
    touch.view.hidden = YES;
}

最新更新