2012-09-10 26 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