2011-12-13 11 views
6

मैं एक पेज मैं एक UIWebView में दिखा रहा हूँ में नल का पता लगाने के लिए जावास्क्रिप्ट का उपयोग कर रहा है, तो जैसे:UIWebView में एक नल का पता लगाने, लेकिन अभी भी समर्थन पाठ चयन और लिंक

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

मैं लिंक में अवरोध उत्पन्न कर रहा हूँ मेरे वेब व्यू प्रतिनिधि के साथ, और "आंतरिक: // टैप" ढूंढें। जब मुझे यह मिलता है, तो मैं वेब दृश्य को नेविगेट करने से रोकता हूं, और टैप का जवाब देता हूं। हालांकि ऐसा करने से मैं पाठ का चयन करने की क्षमता खो देता हूं। लिंक टैप करना अभी भी सही तरीके से काम करता है।

वास्तव में, 'क्लिक' के लिए एक ईवेंट श्रोता जोड़ने से पाठ का चयन करने की क्षमता हटा दी जाती है, भले ही हैंडलर दस्तावेज़ स्थान को बदलने का प्रयास न करे।

कोई विचार क्या मैं गलत कर रहा हूं?

उत्तर

5

स्पष्ट रूप से यदि आप किसी तत्व पर एक क्लिक श्रोता डालते हैं, तो आप अब आईओएस पर उस तत्व के भीतर टेक्स्ट का चयन नहीं कर सकते हैं। मेरा समाधान टचस्टार्ट, टचमोव, और टचचेंड इवेंट्स के संयोजन का उपयोग करके नल का पता लगाने के लिए था, साथ ही बहु-नल को अनदेखा करने के लिए टाइमर के साथ, और यह सुनिश्चित करने के लिए कि वर्तमान चयन चयन नहीं हो रहा है, वर्तमान दस्तावेज़ चयन की जांच कर रहा है।

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

इस के लिए जावास्क्रिप्ट का उपयोग करने के कोई जरूरत नहीं है, यह overkill जब 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

धन्यवाद! यह मेरे लिए मदद की! –

संबंधित मुद्दे