2012-02-18 10 views

答えて

0

UIWebViewはタッチで貪欲であり、すべてを処理したいので、Webビューでジェスチャーを処理するのは難しいです。

いつものようにWebビューでは、Javascriptですべてを試してみることができます。これについてはS.O. postでお読みください。

Iは(ズーム)を挟持し、ただし以下れるウェブビューにスワイプ処理好む方法:

  1. サブクラスUIWindow

    @interface MyWebNavigatorWindow : UIWindow { 
    
  2. のようにそのウィンドウ・タイプのインスタンスをインストールあなたのアプリのウィンドウapplication:didFinishLaunchingWithOptions

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

    また、Interface Builderでウィンドウオブジェクトにクラスを割り当てることもできます。あなたのMyWebNavigatorWindowクラスのsendEvent

  3. ハンドルスワイプやピンチ:

    - (void)sendEvent:(UIEvent*)event { 
        NSSet* allTouches = [event allTouches]; 
        UITouch* touch = [allTouches anyObject]; 
        UIView* touchView = [touch view]; 
    
  4. あなたはタッチがあなたのWebビューの内部で発生したときに検出する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; 
        } 
    
関連する問題