-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 に答える