Pregunta

My question is what algorithm or math should I use to keep the screen swipe momentum TO KEEP SCROLLING if the user does a fast swipe.

Example: There is an image on the screen that revolves in a circle, if the user swipes the camera slow, it will just move along with the finger. However, if the user does it really fast, the image should revolve around like a carousel for few seconds after depending on how fast the user swiped?

I'm able to get the point from when the user touched the screen and the point from where the user kept swiping and lift off the finger, any ideas?

I have a method that is called 60 times a second, so any ideas?

¿Fue útil?

Solución

Basically, you need an initial velocity (measured in points per second, for example) and a deceleration rate (measured in points / second^2). If you use a gesture recognizer to detect a swipe, it will give you the velocity for free. Otherwise you will have to calculate it yourself from the positions and timestamps of the different stages of the swipe.

Now, in your update method, advance your view according to the velocity:

distance travelled = velocity * time

And use the deceleration rate to update the velocity for the next frame:

delta_v = deceleration rate * time // should give a negative value
new velocity = velocity + delta_v

As soon as the velocity drops to a threshold close to 0, stop the motion.

These formulas are for linear movements but even though you are doing a circular motion, I would try experimenting with them. Or you could do the same calculations with angular velocities. Wikipedia probably has the formulas you will have to know.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top