简体   繁体   English

在UIWebView / WKWebView中启用拖动

[英]Enable dragging in UIWebView / WKWebView

Trello, on the desktop website, allows you to drag elements around like this: 桌面网站上的Trello允许您像这样拖动元素:

在此输入图像描述

However, when using Safari on iOS, this doesn't work. 但是,在iOS上使用Safari时,这不起作用。

It either selects the element or pops up a sheet. 它要么选择元素要么弹出工作表。

在此输入图像描述

If I present this in a UIWebView or WKWebView, can I make the WebView act more like desktop Safari, so that I can drag the elements around? 如果我在UIWebView或WKWebView中呈现这一点,我是否可以使WebView更像桌面Safari,以便我可以拖动元素?

I've discovered I can stop various iOS actions by sending the webview some javascript: 我发现我可以通过发送webview一些javascript来停止各种iOS动作:

// stop selection
webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none';")

// stop copy/paste etc
webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitTouchCallout='none';")

// stop loupe
webView.stringByEvaluatingJavaScriptFromString("document.body.style.webkitUserSelect='none';")

But this still doesn't let me drag elements around. 但这仍然不允许我拖动元素。

Is there a solution? 有解决方案吗?

(I know there is a Trello app, I'm just curious if this is possible with WebView) (我知道有一个Trello应用程序,我只是好奇,如果可以使用WebView)

Trello, on the desktop website, allows you to drag elements around.. 桌面网站上的Trello允许您拖动元素。

However, when using Safari on iOS, this doesn't work. 但是,在iOS上使用Safari时,这不起作用。

The reason why it doesn't work on safari iOS is because it considers mouse events(on desktop) different than the touch events(on iOS) 它在Safari iOS上不起作用的原因是因为它认为鼠标事件(在桌面上)与触摸事件不同(在iOS上)

If I present this in a UIWebView or WKWebView, can I make the WebView act more like desktop Safari, so that I can drag the elements around? 如果我在UIWebView或WKWebView中呈现这一点,我是否可以使WebView更像桌面Safari,以便我可以拖动元素?

.. I'm just curious if this is possible with WebView ..我很好奇是否可以使用WebView

Yes it is possible in WebView. 是的,它可以在WebView中使用。 You can use the Jquery UI for drag and drop with an additional library that translates mouse events into touch which is what you need. 您可以使用Jquery UI进行拖放,并使用其他库将鼠标事件转换为触摸,这是您所需要的。 Have a look at jquery-ui-touch-punch . 看看jquery-ui-touch-punch With this your drag and drop from Jquery UI would work on iOS or any device. 有了这个,您从Jquery UI拖放就可以在iOS或任何设备上运行。 You may use something like: 您可以使用以下内容:

function touchHandler(event) {
    var touch = event.changedTouches[0];

    var simulatedEvent = document.createEvent("MouseEvent");
        simulatedEvent.initMouseEvent({
        touchstart: "mousedown",
        touchmove: "mousemove",
        touchend: "mouseup"
    }[event.type], true, true, window, 1,
        touch.screenX, touch.screenY,
        touch.clientX, touch.clientY, false,
        false, false, false, 0, null);

    touch.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}

function init() {
    document.addEventListener("touchstart", touchHandler, true);
    document.addEventListener("touchmove", touchHandler, true);
    document.addEventListener("touchend", touchHandler, true);
    document.addEventListener("touchcancel", touchHandler, true);
}

to convert mouse events into touch and it works like magic. 将鼠标事件转换为触摸,它就像魔法一样。 Have a look at this tutorial on jQuery UI Touch Punch, with jQuery UI . 使用jQuery UI查看关于jQuery UI Touch Punch的教程。 Here's a cool article with demo on the same. 这是一篇很酷的文章与演示相同。

code courtesy 代码礼貌

Well, it is possible with a WebView. 嗯,有可能使用WebView。 The actual answer is : how ? 实际的答案是:怎么样? How, in order to get a great UX result. 如何,以获得一个伟大的用户体验结果。 Mobile device is not a desktop, the actual solution is a matter of UX, dedicated to mobile: 移动设备不是桌面,实际的解决方案是用户体验,专用于移动设备:

You could use a longtap to make a card "flying/ready-for-drag" (turning scroll off). 您可以使用longtap使卡片“飞行/准备拖动”(关闭滚动)。 Or Use a zone/flat-button on the card, that users hook to drag it. 或者使用卡上的区域/平面按钮,用户可以拖动它来拖动它。 Thoughts. 思考。 You should absolutely avoid scroll & drag 'turned on' at the same time. 你绝对应该避免滚动并同时拖动“打开”。 It's drag OR scroll, never drag AND scroll. 它是拖动或滚动,永远不会拖动和滚动。

EDIT You can use : 编辑您可以使用:

In case you are just wondering if there is a simple way to enable drag&drop cross-platform even on mobile, I may suggest you to use Interact.js , a nice UI interaction library I've used successfully in multiple projects. 如果您只是想知道是否有一种简单的方法可以在移动设备上实现拖放跨平台,我建议您使用Interact.js ,这是一个很好的UI交互库,我在多个项目中成功使用。

Using a little code you can achieve a working drag&drop, like this: 使用一些代码,您可以实现工作拖放,如下所示:

var position = { x: 0, y: 0 };
interact('.element')
  .draggable({
    onmove: function(e) {
      var target = e.target;

      // calculate position
      position.x += e.dx;
      position.y += e.dy;

      // translate the element
      target.style.webkitTransform = 'translate(' + position.x + 'px, ' + position.y + 'px)';  
    }    
  });

You can find this working example I've just built here: http://codepen.io/leonardfactory/pen/MwPBjZ 你可以在这里找到我刚刚建立的这个工作示例: http//codepen.io/leonardfactory/pen/MwPBjZ

I've tested it on mobile safari, and it works just fine, allowing drag&drop. 我已经在手机游戏中进行了测试,它运行得很好,允许拖放。 You can find further documentation on its doc page . 您可以在其doc页面上找到更多文档。

I hope this answers your question, if your needs are more specific, feel free to comment and I'll try to edit my answer to adapt it. 我希望这能回答你的问题,如果你的需求更具体,请随意评论,我会尝试编辑我的答案以适应它。

I'm not sure if this is what you need, but take a look at this jquery-ui-touch-punch . 我不确定这是否是你需要的,但看看这个jquery-ui-touch-punch

It handles the touchevents (touchstart, touchmove, touchend) in order to work properly. 它处理touchevents(touchstart,touchmove,touchend)以便正常工作。

Take a look at the demopage, where you can try it out on your mobile device touchpunch demo page . 看一下demopage,你可以在移动设备touchpunch演示页面试一试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM