简体   繁体   English

确定变量是增加还是减少?

[英]Determine if variable is increasing or decreasing?

Basically, I am trying to determine what direction a user is swiping moving their finger on an iOS device using javascript. 基本上,我试图确定用户使用JavaScript在iOS设备上滑动手指的方向。

I know I can figure out where the finger is by using: 我知道我可以使用以下方法找出手指的位置:

e.originalEvent.touches[0].pageX;

So my idea was to 所以我的想法是

  1. Store the location of the first move position, say 130 存储第一个移动位置的位置,例如130

  2. Determine the location of the next position, say 129 确定下一个位置的位置,例如129

  3. If the current position is greater than, it's moving to the right. 如果当前位置大于,则向右移动。 less than, moving to the left 小于,向左移动

The only problem is that it would be running in an event like this: 唯一的问题是它将在以下事件中运行:

$(".container").on("touchmove", function (e) {
    e.preventDefault();
});

So I'm not sure the best way to store the previous location, then the next location, and check to see if they're greater than or less than. 因此,我不确定最好的存储方式是先存储上一个位置,然后再存储下一个位置,并检查它们是否大于或小于。

My original idea was to use something like this: 我最初的想法是使用这样的东西:

$(".container").on("touchstart", function (e) {
    e.preventDefault();
    original = e.originalEvent.touches[0].pageX;
});

$(".container").on("touchmove", function (e) {
    e.preventDefault();
    direction = e.originalEvent.touches[0].pageX;
    if (direction > original) {
        console.log("right");
    } else {
        console.log("left");
    }
});

But that would only determine if the swipe was to the left or right of the origin, not the left or right of the previous finger position. 但这只会确定滑动是在原点的左侧还是右侧,而不是先前手指位置的左侧或右侧。

It looks like you're almost there - you should be able to get the behavior that you want by updating the point that you're comparing to each time the event is called, like this: 看起来您快要到了-每次调用事件时,您都应该能够通过更新要比较的点来获得所需的行为,如下所示:

$(".container").on("touchstart", function (e) {
    e.preventDefault();
    lastPosition = e.originalEvent.touches[0].pageX;
});

$(".container").on("touchmove", function (e) {
    e.preventDefault();
    currentPosition = e.originalEvent.touches[0].pageX;
    if (currentPosition > lastPosition) {
        console.log("right");
    } else {
        console.log("left");
    }
    lastPosition = currentPosition;
});

That said, depending on the platform and what you are trying to achieve, you might find that simply comparing the current position against the previous position gives results that are too "noisy" (ie because the user's finger isn't touching just one pixel, you might see output like "right", "right", "left", "right", "right", "left", "right", ..., when the user is slowly moving their finger right). 就是说,根据平台和您要实现的目标,您可能会发现,仅将当前位置与先前位置进行比较,会得到“噪音很大”的结果(即,因为用户的手指不仅仅触摸一个像素,当用户缓慢向右移动手指时,您可能会看到类似“右”,“右”,“左”,“右”,“右”,“左”,“右”,...的输出)。 If that happens, you might want to do something like record the last 5 previous positions and compare them, like this: 如果发生这种情况,您可能想要做一些事情,例如记录前五个位置并进行比较,如下所示:

var positions = [];
$(".container").on("touchstart", function (e) {
    e.preventDefault();
    positions.push(e.originalEvent.touches[0].pageX);
});

$(".container").on("touchmove", function (e) {
    e.preventDefault();
    positions.push(e.originalEvent.touches[0].pageX);

    var direction = 0;
    var i;
    for (i = 0; i < positions.length - 1; i++) {
        if (positions[i + 1] > positions[i]) {
            direction++;
        } else {
            direction--;
        }
    }

    if (direction > 0) {
        console.log("right");
    }
    else {
        console.log("left");
    }

    if (positions.length > 5) {
        positions.shift();
    }
});

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

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