如何拥有多个UIWebview,每个都有自己的UITapGestureRecognizer?

时间:2014-02-13 09:20:57

标签: ios objective-c uiwebview uigesturerecognizer

我有一个3 UIWebViews的视图控制器,所有视图控制器相互重叠。我使用三个按钮在它们之间切换(类似于段控件)。我想在webview上附加一个轻敲手势识别器,以便点击每个webview导致一个新的控制器。但是,只有第一个webview的UITapGestureRecognizer才会触发。任何人都可以告诉我如何让其他两个手势识别器解雇吗?这是我的代码:

- (IBAction)changeWebView:(id)sender
{
    UIButton *button = (UIButton *)sender;

    switch (button.tag) {
        case 0:
        {
            [self.view bringSubviewToFront:WebView1];

            for (UITapGestureRecognizer *recognizer in WebView2.gestureRecognizers) {
                [WebView2 removeGestureRecognizer:recognizer];
            }

            for (UITapGestureRecognizer *recognizer in WebView3.gestureRecognizers) {
                [WebView3 removeGestureRecognizer:recognizer];
            }


            UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showFullScreen1)];
            tapGestureRecognizer.delegate = self;
            [WebView1 addGestureRecognizer:tapGestureRecognizer];

        }
            break;
        case 1:
        {
           [self.view bringSubviewToFront:WebView2];

            for (UITapGestureRecognizer *recognizer in WebView1.gestureRecognizers) {
                [WebView1 removeGestureRecognizer:recognizer];
            }

            for (UITapGestureRecognizer *recognizer in WebView3.gestureRecognizers) {
                [WebView3 removeGestureRecognizer:recognizer];
            }

            UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showFullScreen2)];
            tapGestureRecognizer.delegate = self;
            [WebView2 addGestureRecognizer:tapGestureRecognizer];
        }
            break;
        case 2:
        {
            [self.view bringSubviewToFront:WebView3];

            for (UITapGestureRecognizer *recognizer in WebView1.gestureRecognizers) {
                [WebView1 removeGestureRecognizer:recognizer];
            }

            for (UITapGestureRecognizer *recognizer in WebView2.gestureRecognizers) {
                [WebView2 removeGestureRecognizer:recognizer];
            }

            UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showFullScreen3)];
            tapGestureRecognizer.delegate = self;
            [WebView3 addGestureRecognizer:tapGestureRecognizer];
        }
            break;

        default:
            break;
    }
}

2 个答案:

答案 0 :(得分:0)

这听起来像是一个设计问题。你为什么不只有一个UIView并根据按下的按钮呈现正确的内容。然后,您可以只有一个手势识别器,它将根据按钮加载正确的控制器(或执行正确的segue)。

答案 1 :(得分:0)

将UITapGestureRecognizer添加到视图中并将所有3个UIWebview作为子视图添加到它们上。每当您检测到要点击的UITapGestureRecognizer时,从segmentedController或按钮获取标记,无论您使用什么并找到哪个被点击。

不要添加UITapGestureRecognizer并一次又一次地删除它。