0

我想要一个非常简单的事情 - 在我的顶级控制器(这是导航控制器)中设置一个水龙头手势识别器,它可以捕捉视图上的所有水龙头。目前,当我点击一个按钮时,系统甚至不会打扰我的识别器(除了gestureRecognizer:shouldReceiveTouch:委托方法,我在那里返回YES)。相反,它只是执行按钮点击。所以我想在视图层次结构上安装“最强”的识别器,无论如何。iOS自来水识别器捕捉所有水龙头

+0

当某人按下按钮时,是否要让水龙头和按钮同时起火? –

+0

https://developer.apple.com/library/prerelease/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/multitouch_background/multitouch_background.html – user523234

+0

不是,每次只有一个人在工作。在我的情况下 - 我打开一个只占用屏幕一部分的抽屉,其余的UI在它下面。然后我在抽屉外打水,然后关闭。当下面的布局中有一个按钮时,它会窃取我的水龙头。 – frangulyan

回答

1

您可以尝试在所有其他视图之上放置一个空的UIView,并向其中添加UITapGestureRecognizer。我们通过帮助叠加来做类似的事情。最大的问题是弄清楚如何以及何时忽略触摸,以便在需要时底层按钮可以获得它们。

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIButton *b = [UIButton buttonWithType:UIButtonTypeInfoDark]; 
    b.frame = CGRectMake(50,50, b.bounds.size.width, b.bounds.size.height); 
    [self.view addSubview:b]; 

    UIView *invisibleView = [[UIView alloc] initWithFrame:self.view.bounds]; 
    invisibleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [invisibleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHit:)]]; 
    [self.view addSubview:invisibleView]; 
} 

-(void)tapHit:(UITapGestureRecognizer*)tap { 
    NSLog(@"tapHit"); 
} 
@end 
相关问题