2012-02-18 102 views

回答

0

處理在Web視圖上發生的手勢是非常棘手的,因爲UIWebView真的很貪婪並且想要處理它們。

像往常一樣使用網絡瀏覽器,你可以嘗試使用Javascript來做所有事情。您可以在S.O. post中閱讀關於它的內容。

我更喜歡處理捏(變焦),雖然刷上的Web視圖的方式是以下幾點:

  1. UIWindow

    @interface MyWebNavigatorWindow : UIWindow { 
    
  2. 安裝窗型的實例適用於您的應用的窗口application:didFinishLaunchingWithOptions

    _window = [[MyWebNavigatorWindow alloc] initWithFrame:rect]; 
    

    或者,您可以在Interface Builder中爲窗口對象分配一個類。

  3. 手柄滑動和捏在sendEventMyWebNavigatorWindow類:

    - (void)sendEvent:(UIEvent*)event { 
        NSSet* allTouches = [event allTouches]; 
        UITouch* touch = [allTouches anyObject]; 
        UIView* touchView = [touch view]; 
    
  4. 您將需要一個機制sendEvent當觸摸你的網頁視圖內發生的檢測。就我而言,我「註冊」的一切,我想處理觸摸的再檢查的意見,如果觸摸裏面他們:

     for (UIView* view in self.controlledViews) { // self.controlledViews is the array of all "registered" views 
         if ([touchView isDescendantOfView:view]) { 
    
  5. 然後我處理各種觸摸階段檢測到什麼樣的姿勢呢是(代碼片段不可編譯,但它提供了這個想法):

     if (touch.phase == UITouchPhaseBegan) { 
         NSLog(@"TOUCH BEGAN"); 
         _initialView = touchView; 
         startTouchPosition1 = [touch locationInView:self]; 
         startTouchTime = touch.timestamp; 
    
         if ([allTouches count] > 1) { 
          startTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 
          previousTouchPosition1 = startTouchPosition1; 
          previousTouchPosition2 = startTouchPosition2; 
         } 
        } 
    
        if (touch.phase == UITouchPhaseMoved) { 
         NSLog(@"TOUCH MOVED"); 
         if ([allTouches count] > 1) { 
          CGPoint currentTouchPosition1 = [[[allTouches allObjects] objectAtIndex:0] locationInView:self]; 
          CGPoint currentTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 
    
          CGFloat currentFingerDistance = CGPointDist(currentTouchPosition1, currentTouchPosition2); 
          CGFloat previousFingerDistance = CGPointDist(previousTouchPosition1, previousTouchPosition2); 
          if (fabs(currentFingerDistance - previousFingerDistance) > ZOOM_DRAG_MIN) { 
           NSNumber* movedDistance = [NSNumber numberWithFloat:currentFingerDistance - previousFingerDistance]; 
           if (currentFingerDistance > previousFingerDistance) { 
            NSLog(@"zoom in"); 
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_IN object:movedDistance]; 
           } else { 
            NSLog(@"zoom out"); 
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_OUT object:movedDistance]; 
           } 
          } 
         } 
        } 
    
        if (touch.phase == UITouchPhaseEnded) { 
         CGPoint currentTouchPosition = [touch locationInView:self]; 
    
         // Check if it's a swipe 
         if (fabsf(startTouchPosition1.x - currentTouchPosition.x) >= SWIPE_DRAG_HORIZ_MIN && 
          fabsf(startTouchPosition1.x - currentTouchPosition.x) > fabsf(startTouchPosition1.y - currentTouchPosition.y) && 
          touch.timestamp - startTouchTime < 0.7 
          ) { 
          // It appears to be a swipe. 
          if (startTouchPosition1.x < currentTouchPosition.x) { 
           NSLog(@"swipe right"); 
           [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_RIGHT object:touch]; 
          } else { 
           NSLog(@"swipe left"); 
           [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_LEFT object:touch]; 
          } 
         } 
         startTouchPosition1 = CGPointMake(-1, -1); 
         _initialView = nil; 
        }