2012-09-10 28 views
-1

我有一個視圖,上面有一些表格和按鈕,然後我想爲整個視圖添加一個水龍頭手勢,但我只想讓那個手勢識別器識別水龍頭。如何在向視圖添加另一個輕擊手勢後禁用其他觸摸手勢?

理想情況下,我想在點擊添加的手勢識別器時執行某些操作,然後在可以訪問其他按鈕和表格後移除該手勢識別器。基本上,點擊即可解除複製類似facebook通知窗口之類功能的功能,輕按外部即可關閉,但不會干擾通知視圖外部的按鈕。

任何人都可以幫忙嗎?

我當前的代碼是:

NotificationsWindow *customView = [[[NSBundle mainBundle]loadNibNamed:@"NotificationsWindow" owner:self options:nil]objectAtIndex:0]; 
    customView.frame= CGRectMake(12, 12, customView.frame.size.width, customView.frame.size.height); 

    UITapGestureRecognizer *recognizerForSubView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehindAgain:)]; 
    [recognizerForSubView setNumberOfTapsRequired:1]; 
    recognizerForSubView.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view 
    [customView addGestureRecognizer:recognizerForSubView]; 


    [self.view addSubview:customView]; 

    [self catchTapForView:customView.superview]; 

(void)handleTapBehind:(UITapGestureRecognizer *)sender 
{ 
    NSLog(@"tapped"); 

    [[self.view.subviews lastObject] removeFromSuperview]; 
    [self.view removeGestureRecognizer:sender]; 
} 

    (void)dismissButton:(UIButton *)button { 
     [button removeFromSuperview]; 
     [[self.view.subviews lastObject] removeFromSuperview]; 

    } 

    (void)catchTapForView:(UIView *)view { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = view.bounds; 
     [button addTarget:self action:@selector(dismissButton:) forControlEvents:UIControlEventTouchUpInside]; 
     [view addSubview:button]; 
    } 

我想,這樣的超級視圖識別駁回子視圖,使之,但不與超視其他水龍頭干擾。

回答

2

做你所描述的最簡單的方法是疊加一個捕捉觸摸的透明UIView。觸摸時只需移除它。

您可以使用下面的代碼:在你看來

- (void)dismissButton:(UIButton *)button { 
    [button removeFromSuperview]; 
} 

- (void)catchTapForView:(UIView *)view { 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = view.bounds; 
    [button addTarget:self action:@selector(dismissButton:) forControlEvents:UIControlEventTouchUpInside]; 
    [view addSubview:button]; 
} 

呼叫catchTapForView。在dismissButton中做任何額外的處理。

+0

將活動識別器保留爲成員,並執行removeGestureRecognizer。 – ggfela

+0

我剛剛添加了我正在使用的代碼,該代碼可以解散,但仍然會干擾超級視圖的代碼,即在超級視圖上按下按鈕會取消子視圖,但也會執行該按鈕應該執行的操作 –

+0

@HudsonDuan請參閱編輯。 – rasmus