jerrybendy / vue-touch-events

Support simple touch events (tap / swipe / touch hold)
MIT License
537 stars 51 forks source link

The `moving` event fires forever #58

Closed Matt-Deacalion closed 4 years ago

Matt-Deacalion commented 4 years ago

When initiating a touch, the moving event fires continuously as I move around. It continues to fire after I end the touch. Should the moving event listener be removed at end?

jerrybendy commented 4 years ago

Is there any troubles if the moving event still bound?

Matt-Deacalion commented 4 years ago

It wouldn't make unnecessary calls to the moving callback, making the code more efficient. The main reason though, is that it feels more obvious that the moving callback should only be concerned with movement when "swiping".

An example would be if we wanted an element to follow the "cursor", we could set the element's x and y within the moving callback and we're done. But if moving is always called, we have to bind it in start and then unbind it on end or else the element will continue tracking the cursor.

jerrybendy commented 4 years ago

I can't understand your problem clearly. I'm not good at English in fact. Reffer to https://github.com/jerrybendy/vue-touch-events/blob/master/index.js#L112 , only when touchMoved is true, the moving event will be triggered. And the touchMoveEvent function is called when native touchmove or mousemove event triggered. So I think your problem doesn't exist. Moving event wouldn't be triggered when you release your finger from a touch screen.

Matt-Deacalion commented 4 years ago

You may be right. The README.md does say that moving is a "Continuously triggering Event". I got confused because it doesn't track movement from the beginning.

Example: https://jsfiddle.net/kvfm8xsr/

jerrybendy commented 4 years ago

Moving often works with start and end. In your example, you should set clicked to true when start triggered and set to false when end triggered.

<div
  v-touch:start="() => this.clicked = true"
  v-touch:end="() => this.clicked = false"
  v-touch:moving="movingHandler"
></div>
movingHandler: function (e) {
        if (this.clicked) {
        [this.x, this.y] = [e.x - 25, e.y - 75];
        }
}