在UIWebView中检测单击,但仍支持文本select和链接

我正在使用JavaScript来检测我在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> 

我拦截与我的networking查看代理的链接,并寻找“内部://点击”。 当我得到这个,我阻止networking视图导航,并响应水龙头。 但是,这样做,我失去了select文本的能力。 点击链接仍然正常工作。

事实上,只要添加一个“点击”事件监听器,即使处理程序不尝试更改文档位置,也会删除select文本的function。

任何想法我做错了什么?

很明显,如果你在一个元素上放置一个点击监听器,你就不能再在iOS上select该元素中的文本。 我的解决scheme是使用touchstart,touchmove和touchend事件的组合来检测水龙头,还有一个定时器忽略多水龙头,并检查当前的文档select以确保select事件不会发生。

这里是我使用的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"; }); 

没有必要为此使用Javascript,当UIGestureRecognizerDelegate有足够的方法时,这是过度的。 所有你需要做的是确保当文本select发生时,轻拍识别器不被触发。

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

这需要照顾文本select,并且链接应该工作正常(至less他们为我做)。