如何在视图中添加另一个点击手势后禁用其他触摸手势?

时间:2012-09-10 21:19:38

标签: ios xcode notifications uigesturerecognizer

我有一个带有一些表格和按钮的视图,然后我想在整个视图中添加一个轻击手势,但我只想让手势识别器识别点击。

理想情况下,我想在点击添加的手势识别器时执行某些操作,然后删除该手势识别器,以便可以访问其他按钮和表格。基本上是点击以解除复制像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];
    }

我想这样做,以便超级视图的识别器解除子视图,但不会干扰超级视图上的其他点击。

1 个答案:

答案 0 :(得分: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中进行任何其他处理。