2011-12-13 34 views
6

Tôi đang sử dụng JavaScript để phát hiện vòi trong một trang Tôi đang hiển thị trong một UIWebView, như vậy:Detect vòi duy nhất trong UIWebView, nhưng vẫn hỗ trợ lựa chọn văn bản và liên kết

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

Tôi chặn liên kết với ủy quyền xem web của tôi và tìm "internal: // tap". Khi tôi nhận được điều đó, tôi ngăn chế độ xem web khỏi điều hướng và phản hồi lại lần nhấn. Tuy nhiên làm điều này tôi mất khả năng chọn văn bản. Khai thác liên kết vẫn hoạt động chính xác.

Thực tế, chỉ cần thêm trình xử lý sự kiện để 'nhấp' sẽ loại bỏ khả năng chọn văn bản, ngay cả khi trình xử lý không cố gắng thay đổi vị trí tài liệu.

Bất kỳ ý tưởng nào tôi đang làm sai?

Trả lời

5

Dường như nếu bạn đặt trình nghe nhấp chuột vào một phần tử, bạn không còn có thể chọn văn bản trong phần tử đó trên iOS nữa. Giải pháp của tôi là phát hiện các vòi bằng cách sử dụng kết hợp các sự kiện chạm, chạm và chạm, cùng với bộ hẹn giờ để bỏ qua nhiều lần nhấn và kiểm tra lựa chọn tài liệu hiện tại để đảm bảo sự kiện lựa chọn không diễn ra.

Dưới đây là đoạn code JS Tôi đã sử dụng:

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

Không có nhu cầu sử dụng Javascript cho điều này, đó là quá mức cần thiết khi UIGestureRecognizerDelegate có phương pháp thích hợp. Tất cả những gì bạn cần làm là đảm bảo rằng khi lựa chọn văn bản đang diễn ra, trình nhận dạng nhấn không được kích hoạt.

- (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; 
} 

Điều đó sẽ quan tâm đến lựa chọn văn bản và liên kết sẽ hoạt động tốt (ít nhất là họ làm cho tôi).

+0

Cảm ơn bạn! Nó đã giúp tôi! –

Các vấn đề liên quan