2014-02-13 99 views
0

我有一個視圖控制器與3 UIWebViews,所有幀都覆蓋在一起。我使用三個按鈕在它們之間切換(類似於段控制)。我想附加一個輕敲手勢識別器到web視圖,以便點擊每個webview導致一個新的控制器。但是,只有第一個webview的UITapGestureRecognizer纔會觸發。任何人都可以告訴我如何讓另外兩個手勢識別器開火嗎?這是我的代碼:如何擁有多個UIWebviews,每個都有自己的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; 
    } 
} 

回答

0

這聽起來像是一個設計問題。你爲什麼不只有一個UIView,並根據按下哪個按鈕來呈現正確的內容。您可以只有一個手勢識別器,根據按鈕也可以加載正確的控制器(或執行正確的輪巡)。

+0

感謝您的回覆,但我需要一次加載所有三個頁面,並且我還希望啓用Web視圖的縮放功能。所以我不能使用單個視圖,甚至不能在webviews上疊加一個透明的UIButton。 – dragonfly

0

將UITapGestureRecognizer添加到視圖中,並將全部3個UIWebview作爲子視圖添加到它們上。只要您檢測到要輕觸的UITapGestureRecognizer,就可以從segmentedController或按鈕中獲取標記,然後查找您正在使用的哪一個,並找到哪一個是輕擊的。

請勿添加UITapGestureRecognizer並一次又一次刪除它。

相關問題