-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];
    }

我想让它让超级视图的识别器关闭子视图,但不干扰超级视图上的其他点击。

4

1 回答 1

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

于 2012-09-10T21:25:08.460 回答