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 
相關問題