【问题标题】:How to capture mouseDown Events in a WebView?如何在 WebView 中捕获 mouseDown 事件?
【发布时间】:2012-05-21 17:01:40
【问题描述】:

我想处理WebView 中的mouseDown 事件。我继承了WebView,实现了mouseDown,我知道mouseDown被重定向到WebHTMLView

我已经浏览了以下链接。

mouseDown: not firing in WebView subclass

从上面的链接我了解到,我们可以将 mouseDownEvent 重定向到 Objective-C,但我没有找到任何解决方案。我可以有任何指针或示例 javascript 来实现相同的。

我在webview子类中尝试了hitTest方法。

- (NSView*)hitTest:(NSPoint)aPoint

上面的方法可以识别mouseDown吗?

【问题讨论】:

    标签: objective-c macos cocoa webview webkit


    【解决方案1】:

    您也可以使用 onTouchStart 事件。请参阅此link to bind events on iphone

    或者使用这个:-

    $('button').bind( "touchstart", function(){alert('hey'} );
    

    touchstart 相当于 mouseDown

    另一种解决方案是将元素的光标设置为指针,它可以与 jQuery live 和 click 事件一起使用。在 CSS 中设置它。(cursor:pointer)

    你可以在 JavaScript 中使用:-

    node.ontouchmove = function(e){
      if(e.touches.length == 1){ // Only deal with one finger
        var touch = e.touches[0]; // Get the information for finger #1
        var node = touch.target; // Find the node the drag started from
        node.style.position = "absolute";
        node.style.left = touch.pageX + "px";
        node.style.top = touch.pageY + "px";
      }
    }
    

    document.addEventListener('touchstart', function(event) {
        alert(event.touches.length);
    }, false);
    

    更多详情请参考javascript iPhone Events

    对于 Mac OSx,请参阅 The Link

    【讨论】:

    • corona zorro : 这不是 iPhone。你对同样的 Cocoa 有什么建议吗?
    • Cocoa Touch 是一个 UI 框架,用于构建可在 Apple Inc. 的 iPhone、iPod Touch 和 iPad 上运行的软件程序。因此,它也可以在这些设备上运行,它只是 iPhone 的一部分。
    【解决方案2】:

    我使用以下 HTML 和 JavaScript 来捕获 mouseEvents 并在 WebView 中显示鼠标单击的位置。

     <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script type="text/javascript">
        function SetValues()
        {
            var s = 'X=' + window.event.clientX +  ' Y=' + window.event.clientY ;
            document.getElementById('divCoord').innerText = s;
    
        }  
    </script>
    </head>
    <body id="bodyTag" onclick=SetValues()>
    <div id="divCoord"></div>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2014-01-31
      相关资源
      最近更新 更多