Javascript使用gesturechange和gestureend进行缩放和旋转

发布于 2024-11-17 06:45:34 字数 1833 浏览 2 评论 0原文

我正在处理一些触摸事件和手势,我希望能够缩放和旋转对象,我已成功使其可拖动,但手势给我带来了麻烦。手势可以工作,但不稳定,每当您捏住它来放大或缩小或尝试旋转它时,它就会从一个手指跳到另一个手指。

这是我的代码供参考。

var width = 300; var height = 300; var rotation = 0;
$('.dynamic').live("gesturechange gestureend", function(e){
var orig = e.originalEvent;

if(e.type == 'gesturechange'){
    e.preventDefault();
    $(this).css("width", parseFloat(width) * orig.scale);
    $(this).css("height", parseFloat(height) * orig.scale);
    $(this).css("-webkit-transform","rotate(" + ((parseFloat(rotation) + orig.rotation) % 360) + "deg)");
}else if(e.type == 'gestureend'){
    a.w[ids] = parseFloat(width) * orig.scale;
    a.h[ids] = parseFloat(height) * orig.scale;
    a.rotation[ids] = (parseFloat(rotation) + orig.rotation) % 360;
}
});

无论如何,有没有办法让它变得顺利,并防止它从手指上跳下来,或者我采取的方法是错误的。需要一些提示和技巧并帮助

找到解决方案

似乎我的拖动触摸事件干扰了手势,这就是它不断从一个手指跳到另一个手指的原因,解决这个问题的方法是不使用手势,而是计算对象上的触摸次数并使用触摸开始、结束和更改。

这是代码

var touches = 0; var width = 300; var height = 300; var rotation = 0;
$('.dynamic').live("touchstart touchmove touchend", function(e){
var orig = e.originalEvent;   

if(e.type == 'touchstart'){
 if(orig.touches.length == 1){
    touches = 1; 
 }else if(orig.touches.length == 2){
    touches = 2;
 }
}else if(e.type == 'touchmove'){
 e.preventDefault();
 if(touches == 2){
        $(this).css("width", parseFloat(width) * orig.scale);
        $(this).css("height", parseFloat(height) * orig.scale);
        $(this).css("-webkit-transform","rotate(" + ((parseFloat(rotation) + orig.rotation) % 360) + "deg)");
 }
}else if(e.type == 'touchend'){
    if(touches == 2){
        a.w[ids] = parseFloat(width) * orig.scale;
        a.h[ids] = parseFloat(height) * orig.scale;
        a.rotation[ids] = (parseFloat(rotation) + orig.rotation) % 360;
    }
}
});

I am working on some touch events and gestures, I want to be able to zoom and rotate the object, I have successfully made it draggable but the gestures are giving me trouble. The gestures are working but they are choppy, whenever you pinch it to zoom in or zoom out or try to rotate it, it jumps from finger to finger.

Here is my code for reference.

var width = 300; var height = 300; var rotation = 0;
$('.dynamic').live("gesturechange gestureend", function(e){
var orig = e.originalEvent;

if(e.type == 'gesturechange'){
    e.preventDefault();
    $(this).css("width", parseFloat(width) * orig.scale);
    $(this).css("height", parseFloat(height) * orig.scale);
    $(this).css("-webkit-transform","rotate(" + ((parseFloat(rotation) + orig.rotation) % 360) + "deg)");
}else if(e.type == 'gestureend'){
    a.w[ids] = parseFloat(width) * orig.scale;
    a.h[ids] = parseFloat(height) * orig.scale;
    a.rotation[ids] = (parseFloat(rotation) + orig.rotation) % 360;
}
});

Are there anyways to make this smooth, and prevent it from jumping from fingers, or is the approach I took wrong. In need of some tips and tricks and help

found a solution

Seems like the my touch event for drag interfered with the gestures thats why it kept jumping from finger to finger, the way around this was to not use gestures instead count the touches on the object and use touch start,end and change instead.

Here is the code

var touches = 0; var width = 300; var height = 300; var rotation = 0;
$('.dynamic').live("touchstart touchmove touchend", function(e){
var orig = e.originalEvent;   

if(e.type == 'touchstart'){
 if(orig.touches.length == 1){
    touches = 1; 
 }else if(orig.touches.length == 2){
    touches = 2;
 }
}else if(e.type == 'touchmove'){
 e.preventDefault();
 if(touches == 2){
        $(this).css("width", parseFloat(width) * orig.scale);
        $(this).css("height", parseFloat(height) * orig.scale);
        $(this).css("-webkit-transform","rotate(" + ((parseFloat(rotation) + orig.rotation) % 360) + "deg)");
 }
}else if(e.type == 'touchend'){
    if(touches == 2){
        a.w[ids] = parseFloat(width) * orig.scale;
        a.h[ids] = parseFloat(height) * orig.scale;
        a.rotation[ids] = (parseFloat(rotation) + orig.rotation) % 360;
    }
}
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活泼老夫 2024-11-24 06:45:34

您的解决方案有效,但不是最优雅的:您仍然可以使用第一段代码并依赖gesture 事件。
您所要做的就是确保touch 事件处理程序不会干扰您的手势 事件处理程序。
只需将其添加到 touch 事件处理程序中:

$('.dynamic').live("touchstart touchmove touchend", function(e){
    if(e.originalEvent.touches.length > 1)
        return;
    /* your touch code here */
});

然后使用现有的手势代码:

$('.dynamic').live("gesturechange gestureend", function(e){
     /* your gesture code here */
});

这应该可以工作,因为只有两个或更多手指触摸屏幕时才会触发手势事件,并且不会执行其他代码发生这种情况是因为触摸事件处理程序仅响应单次触摸。

Your solution works, but it's not the most elegant: you can still use your first piece of code and rely on the gesture events.
All you have to do is make sure that the touch event handlers do not interfere with your gesture ones.
Just add this into the touch event handlers:

$('.dynamic').live("touchstart touchmove touchend", function(e){
    if(e.originalEvent.touches.length > 1)
        return;
    /* your touch code here */
});

and then use your existing gesture code :

$('.dynamic').live("gesturechange gestureend", function(e){
     /* your gesture code here */
});

This should work because the gesture events are triggered only there are two or more fingers touching the screen, and no other code executes when this happens, because the touch event handlers respond only to a single touch.

旧竹 2024-11-24 06:45:34

也许 jquery 不是最适合这项任务的。您可能会考虑使用 sencha touch。它已经很好地完成了您想要的事情。查看演示。根据您想要执行的操作,jQuery Mobile 也是一个不错的选择。

Maybe jquery is not the best for this task. You might think about using sencha touch. It already does what you wanted very nicely. Have a look at the demos. Depending on what you want to do jQuery Mobile would also be a good choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文