javascript
  • iphone
  • ios
  • uiwebview
  • 2011-12-13 5 views 6 likes 
    6

    リンク:はのUIWebViewでシングルタップを検出し、それでもテキスト選択をサポートし、私はそうのように、私はのUIWebViewで表示していますページでタップを検出するために、JavaScriptを使用してい

    <div id="wrapper"> 
        <a href="http://apple.com">Apple</a> 
    </div> 
    <script> 
        document.getElementById("wrapper").addEventListener('click', function() { 
         document.location = 'internal://tap'; 
        }, false); 
    </script> 
    

    私はリンクを傍受しています私のWebビューのデリゲートで "internal:// tap"を探します。私がそれを得ると、私は、Webビューのナビゲートを防ぎ、タップに応答します。しかしこれを行うと、テキストを選択する能力が失われます。リンクをタップしても正しく動作します。

    実際、「クリック」のイベントリスナーを追加するだけで、ハンドラがドキュメントの場所を変更しようとしても、テキストを選択することができなくなります。

    私が間違っていることを知っていますか?

    答えて

    5

    クリックリスナーを要素に配置すると、iOSの要素内でテキストを選択できなくなっているようです。私の解決策は、マルチタップを無視し、現在のドキュメントの選択をチェックして選択イベントが確実に行われていないことを確認するタイマーとともに、タッチスタート、タッチ移動、およびタッチイベントの組み合わせを使用してタップを検出することでした。ここで

    は、私が使用したJSコードです:

    SingleTapDetector = function(element, handler) { 
        this.element = element; 
        this.handler = handler; 
    
        element.addEventListener('touchstart', this, false); 
    }; 
    
    SingleTapDetector.prototype.handleEvent = function(event) { 
        switch (event.type) { 
         case 'touchstart': this.onTouchStart(event); break; 
         case 'touchmove': this.onTouchMove(event); break; 
         case 'touchend': this.onTouchEnd(event); break; 
        } 
    }; 
    
    SingleTapDetector.prototype.onTouchStart = function(event) { 
        this.element.addEventListener('touchend', this, false); 
        document.body.addEventListener('touchmove', this, false); 
    
        this.startX = this.currentX = event.touches[0].clientX; 
        this.startY = this.currentY = event.touches[0].clientY; 
        this.startTime = new Date().getTime(); 
    }; 
    
    SingleTapDetector.prototype.onTouchMove = function(event) { 
        this.currentX = event.touches[0].clientX; 
        this.currentY = event.touches[0].clientY; 
    }; 
    
    SingleTapDetector.prototype.onTouchEnd = function(event) { 
        var that = this; 
    
        // Has there been one or more taps in this sequence already? 
        if (this.tapTimer) { 
         // Reset the timer to catch any additional taps in this sequence 
         clearTimeout(this.tapTimer); 
         this.tapTimer = setTimeout(function() { 
          that.tapTimer = null; 
         }, 300); 
        } else { 
         // Make sure the user didn't move too much 
         if (Math.abs(this.currentX - this.startX) < 4 && 
          Math.abs(this.currentY - this.startY) < 4) { 
          // Make sure this isn't a long press 
          if (new Date().getTime() - this.startTime <= 300) { 
           // Make sure this tap wasn't part of a selection event 
           if (window.getSelection() + '' == '') {      
            // Make sure this tap is in fact a single tap 
            this.tapTimer = setTimeout(function() { 
             that.tapTimer = null; 
    
             // This is a single tap 
             that.handler(event); 
            }, 300); 
           } 
          } 
         } 
        } 
    }; 
    
    new SingleTapDetector(document.body, function(event) { 
        document.location = "internal://tap"; 
    }); 
    
    3

    このためにJavascriptを使用する必要はありませんUIGestureRecognizerDelegateが適切なメソッドを持っているとき、それはやり過ぎだ。テキスト選択が行われているときに、タップ認識機能が起動されていないことを確認するだけです。

    - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
        BOOL hasTap = ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] || 
           [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]); 
        BOOL hasLongTouch = ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || 
            [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]); 
        if (hasTap && hasLongTouch) { 
         // user is selecting text 
         return NO; 
        } 
        return YES; 
    } 
    

    これは、テキスト選択を処理し、リンクはとにかく(少なくとも私にとっては)うまくいくはずです。

    +0

    ありがとうございました!それは私のために助けになった! –

    関連する問題