A detailed and implementation of mobile interactive gestures

Source: Internet
Author: User

I. Overview
Mobile devices are now a big line, and the front end is moving into the field. In writing the interactive effect of the mobile page, I inevitably have to touch some complex gestures, not just like PC-side simple mouse events. Gestures are actually an input pattern. We are now in the intuitive sense of human-computer interaction refers to the interactive way between man and machine, this interactive way through the mouse, physical hardware, screen touch, long-distance body sense of the gradual development of the process of operation.

Two, mobile gesture event
In the browser, there are not many gestures provided for us, mainly:

    1. Touchstart triggered when the finger touches the screen
    2. Touchmove A constant trigger when the finger is sliding on the screen
    3. touchend triggered when the finger moves away from the screen
    4. Touchcancel triggered when the system stops tracking touch

is not feeling very little, Safari also provides us with three unique gesture events (for complex gestures), but it can only be used in Safari

    1.          gesturechange   triggers
    2. when any finger on the screen changes Span style= "font-family: ' Comic Sans MS ', Sans-serif;" >         gestureend   triggers when any one finger moves away from the screen

    
  

Three, let JS support these gestures   

    1. Tap Tap
    2. Doubletap two consecutive taps
    3. Press Long pressed
    4. Pan Pan
    5. flick Flick


First we'll fix how to trigger a custom event (we've learned that custom events can be skipped):

//Customize an event
Document.body.addEventListener ("Tap", function (event) {
Console.log ("tap Event Trigger")
}, False)
//Touch Custom Events
function FireEvent (element, type, extra) {
var event = doc.createevent (' htmlevents ');
event.initevent (Type, true, true);
if (typeof extra = = = = ' object ') {
util.extends (event, extra);//Shallow copy
}
element.dispatchevent (event);
}
fireEvent (Document.body, "tap", {});//trigger Tap event

we define an intermediate state Evet.status in the entire event simulation to represent the current touch state, and then we can use touchstart,touchmove,touchend to implement our own touch events.
Tap event: When Touchstart is triggered, we change the event.status state to tapping.  When Touchend is triggered, the tap event is triggered if Event.status is still tapping.
Doubletap event: When the tap event is triggered, we use a variable lasttime to record the current time. The next time the tap is triggered, it is compared with the lasttime, and the Doubletap event is triggered if less than 300ms
pess event: When Touchstart is triggered, we define a settimeout function (500ms), if there is still no touchend trigger after 500ms, The timer function changes the Event.staus state to pressing.  When the touchend is triggered, the press event is triggered when the status of pressing is detected.
Pan event: When we detect that the current state is tapping and pressing in touchmove and the finger moves more than 10px, the pan translation event is triggered.  This mobile distance is detected with Event.touches[0].clientx-lasttouch.clienty (using Lasttouch record, the event object of the starting finger).
flick event: This event is a "brush ~ brush" across the screen interactive effect, at touchend time by the Pan event movement distance and movement events to calculate the speed (note is the X and Y axis of the speed),  If the speed is greater than 0.5 and the entire touch process time is less than 100ms, the flick event is triggered.
It is not easy to use the most primitive browser events to achieve these things.
next let's look at the two-finger events if implemented.

Four. Achieve multi-finger touch control
when implementing multi-finger touch, we need to understand the three attributes that event uses to hold multiple finger information during a touch:

    1. touches List of all touch points on the current screen
    2. targettouches The collection list of touch points on the node that binds the event
    3. changedtouches The set of touch points that are changed when the event is triggered


What's the difference between these three? For example, for example, Div1, div2 only div2 bound Touchstart event, the first time to drop a finger on the div2, triggering the Touchstart event, this time, the contents of the three collection is the same, all contain the touch of this finger, and then, Put down two fingers one on the div1, one on the div2, this time it will trigger the event, but the changedtouches contains only the second third finger information, because the first one has not changed, The targettouches contains a collection of fingers on the first finger and a third on the DIV2, touches contains information about all the fingers on the screen, that is, three fingers. This is not very clear. Below we will continue to solve a problem based on the above: when two fingers on different nodes should trigger which node of the event?
Here we specify that if the trigger is on two different nodes, we go to the nearest parent node of the two nodes, as the trigger target. Find common minimum parent node code as follows:

//Determine if node Ele1 contains Ele2
function contains (Ele1, ele2) {
return ele1.contains? ele1! = Ele2 && ele1.contains (ele2):!! (Ele1.comparedocumentposition (Ele2) &);
}
//Get a shared nearest parent node
function Getcommonrootnode (ele1, ele2) {
While (ele1) {
if (contains (Ele1, ele2) | | ele1 = = = Ele2) {
return ele1;
}
ele1 = Ele1.parentnode;
}
return null;
}

so we solved the problem of how to find multiple finger information and which node to trigger. The last question, when you give us this information, how can we use it? such as calculating rotation gestures, scaling gestures ah what.   
    Here we only consider multi-touch with two fingers. We set the Touchstart stage with two finger coordinates of a (x1, y1)    b (x2, y2). The coordinates of the two fingers in the touchmove process are C (x3, y3) D (x4, Y4).
     Rotate Rotation: Calculates the angle between the AB,CD segment and the axis, and the angle is subtracted to get the angle of rotation.   
     scale   Zoom: Calculates the length of the AB segment and the length of the CD segment (Pythagorean theorem), the ratio of two lines is good.   
     Translate pan: We only calculate the x-coordinate variation of point A to C when translating.  
    code as follows:

function calcaction (x1, y1, x2, y2, x3, Y3, X4, y4) {
Let rotate = math.atan2 (Y4-y3, x4-x3)-Math.atan2 (Y2-y1, x2-x1),
Scale = Math.sqrt ((Math.pow (Y4-y3, 2) + Math.pow (X4-X3, 2)/(Math.pow (Y2-y1, 2) + Math.pow (x2-x1, 2)),
translate = [X3-scale * x1 * MATH.COS (rotate) + scale * y1 * math.sin (rotate), Y3-scale * y1 * math.cos (rotate)- Scale * X1 * Math.sin (rotate)];
return {
Rotate:rotate,
Scale:scale,
Translate:translate,
/**
* |ax + cy + e|
* |bx + dy + f|
* | 0 + 0 + 1|
*/
Martrix: [
[scale * math.cos (rotate),-scale * Math.sin (rotate), translate[0]],
[scale * math.sin (rotate), scale * MATH.COS (rotate), translate[1]],
[0, 0, 1]
]
}
}

By understanding this, you can encapsulate the interactive effects of two-finger rotation and zoom panning during the touchmove process. Isn't it simple!
Of course, the complete event process to be divided into start,move,end these three cases, in the implementation of the time to give the corresponding implementation is possible. Can not escape the use of touchstart,touchmove,touchend.
V. Implementation cases
Based on the above scenario, I implemented a package library of mobile gestures that contains all of the above gestures. Hesitate to run the code here can't simulate the phone environment, I will not post code.
Interested students can visit: Https://github.com/T-phantom/si-gesture above has a specific use of the method and with detailed comments of the source Oh, welcome to start.

A detailed and implementation of mobile interactive gestures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.