Preface With the richness of hybrid applications, HTML5 engineers are no longer satisfied with simply porting the desktop experience to the mobile terminal. They covet the humanized operation experience of mobile native applications, especially the rich gesture system that native applications are born with. HTML5 does not provide an out-of-the-box gesture system, but it provides a more basic monitoring of touch events. Based on this, we can make our own gesture library. gesture Commonly used HTML5 gestures can be divided into two categories, single-point gestures and two-point gestures. Single-point gestures include tap, double tap, long tap, swipe, and move. Two-point gestures include pinch and rotate. Next, we will implement a javascript library to detect these gestures and use this gesture library to create cool interactive effects. move We will not go into details about mobile gesture detection here. To sum up, every time a touchmove event occurs, the coordinate positions between the two displacement points are subtracted. Click (tap) The key to gesture detection is to decompose gestures using three events: touchstart, touchmove, and touchend. So how do you decompose the click event?
With the above process, you can start to implement tap event monitoring.
Double tap Like single click, double click event also requires us to quantify and decompose the gesture.
Note that in the double-click event we detect the displacement and time interval between two adjacent touchstart events.
Long press Long press is probably the easiest gesture to decompose. We can decompose it like this: if no touchmove or touchend event occurs for a long period of time after touchstart occurs, then a long press gesture is triggered. Long press is a one-finger action that requires detecting whether there is only one contact point on the screen. If the finger moves in space, the long press event is canceled. If the finger stays on the screen for more than 800ms, a long press gesture is triggered. If the finger stays on the screen for less than 800ms, that is, touchend is triggered within 800ms after touchstart occurs, the long press event is canceled.
Pinch Zooming is a very interesting gesture. Do you still remember the shock brought by pinching to zoom pictures on the first generation iPhone? Even so, the detection of zooming gesture is relatively simple. Zooming is a two-finger behavior and requires detecting whether there are two contact points on the screen. The quantification of the scaling ratio is obtained by the ratio of the distances between two scaling actions, as shown in the following figure. So the core of scaling is to get the straight-line distance between two contact points.
Here xLen is the absolute value of the difference in x coordinates of the two contact points, and yLen is the absolute value of the difference in y coordinates.
In the _onTouchStart function, get and save the distance between the two contact points when touchstart occurs.
Rotate The rotation gesture needs to detect two important values: the rotation angle and the rotation direction (clockwise or counterclockwise). The calculation of the rotation angle and direction needs to be obtained through vector calculation, which will not be expanded in this article. First, you need to get the rotation direction and angle of the vector.
Then, when the finger moves, we call the method to obtain the rotation direction and angle.
Actual Combat Well, our gesture system is now complete. Next, we need to test whether this system is reliable in actual combat and make a simple image browser that supports image zooming, rotation, movement, and long pressing. First, make a good DOM plan. Just like "before", our event monitoring mechanism does not act directly on the image, but on the parent element of the image. Then, you can start using the gesture detection system above.
Because our gesture system detects increments, we cannot directly apply the increments to the object, but need to accumulate these increments. Take rotation as an example:
At this point, our gesture detection is complete. |
<<: You can also play like this: Check out Siri's 12 new features in iOS 11
>>: How to solve the multi-body problem in deep learning
Regarding the issue of growth, there is a vicious...
introduction In 1971, Kevin Kelly dropped out of ...
15-Day Full Body Fat Burning Transformation Camp ...
A must-learn for Pinduoduo merchants to open a st...
Can you tell the difference between the final tit...
Meizu MX4 was launched. This mobile phone launch ...
A file transfer assistant that can be used with a...
The three most critical steps in live streaming s...
Review expert: Li Xianhong, national second-level...
Editor’s Note: Compared with the enthusiasm of Ce...
When placing homogeneous advertisements in inform...
"Forrest Gump" said: "Life is like...
For WEY, a high-end brand under Great Wall Motors...
Editor’s Note: Immune cells are responsible for r...
The Android system provides a variety of inter-pr...