The bottom of the browser does not provide similar to the elements, click, double-tap, swipe, drag these directly can use the control interface, all gestures can only be simulated out. The only thing that the mobile browser gives us is the three most basic event interfaces, MouseDown-MouseMove---MouseUp. Then we can only simulate complex gestures out of these simple interfaces.
General procedure Flow:
1. Bind three events to an element, MouseDown, Mousemove,mouseup
2. At the time of interaction, the user only triggers Mousedown,mouseup no trigger MouseMove, it can be a single click action, here can be clicked, double-click and long press, can be determined by the interval of time
3. If the MouseMove is triggered, it is natural to slide and drag
Of course the gesture also involves the processing of multi-point operation scaling and rotation, which is discussed later.
Before we leave the process, we need to know that there are at least 2 points to consider when designing a gesture library:
1. Running the platform
2. User's gestures
Then we can conclude that there are three kinds of terminal equipment in the market:
1 Mobile/pad Mobile Terminal
2 PC Class
3 There is a kind of computer with touch screen
The user's gesture behavior is broadly divided:
Click Tap, double-click Doubletap, pan pan, slide swipe, press long, zoompinch,旋转rotate
From the point of view of design, there are different compatibility and choice problems, but have some of the same commonality and processing how do we go about organizational structure?
Of course the OOP is still designed, extracting the parent class, implementing the inheritance, introducing the policy mode
Let's see how hammer is structured to achieve this kind of design.
In general, the processing of gestures is divided into initialization and execution period. Build all relevant participation and methods when initializing
Hammer source is divided into several chunks:
1. The hammer class, a simple factory method, is used to create a default recognizer that manages and initializes. Hammer.defaults Configure some of the basic options
Includes settings for each recognizer's configuration and element CSS properties
2. Manager class, management class for the entire library. Internally initialized input input object, all gesture object, element CSS setting object touchaction
3. Inputhandler class, event callback specific processing class, used to generate the wrapped event object and dispatch events to each recognizer
4. Touchaction class, setting the Touchaction property of an element
5. Input class, event handler class. The callback used to handle binding and destroying the event handle. Each input class needs to inherit
6. Recognizer class, all recognizers need to inherit the base class
The above is the entire library of class blocks, of course, 5 and 6 are the base class inheritance, the code is run when the automatic construction is complete
About inheriting inherit methods
Hammer is a traditional similar inheritance.
function inherit (child, base, properties) { var basep = base.prototype, childp; = Child.prototype = object.create (basep); = Child ; = basep; if (properties) { Extend (CHILDP, properties);} }
Only the methods of the prototype are inherited, because the prototypes are shared, and if the placement attributes can be modified by any one of the inherited subclasses, the inheritance of the attributes requires the call method
The inherited subclass has a private attribute _super points to the parent class, as well as an additional extension method
Inherit (Mouseinput, Input, { function mehandler (EV) { }}}
Subclass Mouseinput inherits all the prototype methods of the parent input class and extends the handler method
Input device initialization inheritance: What device triggers the action (PC, phone, ipad, etc.)
Input device hammer is divided into
Mouseinput,pointereventinput,singletouchinput,touchinput,touchmouseinput
Let's look at the simplest desktop PC's mouse input processing mouseinput, the rest of the structure is basically similar.
functionMouseinput () { This. Evel =mouse_element_events; This. Evwin =mouse_window_events; //used to prohibit Touchmouse events This. Allow =true;//used by Input.touchmouse to disable mouse events //the state of the mouse pressed This. pressed =false;//MouseDown StateInput.apply ( This, arguments);} Inherit (Mouseinput, Input, {handler:functionMehandler (EV) {....////////////// }}
Mouseinput initializes several of the necessary judgment properties, then just handler the method, and also integrates input input class
For example, we call
At New Mouseinput (callback), the base class input class is initialized by Input.apply (this, arguments), then the init inside the base class is bound to the event, and the callback of the event is
this. Domhandler points to the external callback callback, which is actually the handler method.
Addeventlisteners (this this. domhandler);
Another base class is recognizer.
Because we divide the user's behavior by clicking Tap, double-clicking Doubletap, panning Pan, sliding swipe, long press, zoompinch,旋转rotate,那么类似相同点我们也必须抽象成一个基类
Recognizer such as complexity, stay in the implementation period of time to explain.
Hammer using:
var New Hammer (EL);
So the internal building
function Hammer (element, options) { = Options | | {}; // configuring gesture recognizer Parameters Options.recognizers = ifundefined (options.recognizers, Hammer.defaults.preset); return New Manager (element, options);}
Visible is finally the object instance that the manager built.
Inside manager, create an instance object of the input environment through createinputinstance, creating an instance object for the input environment
this. Input = Createinputinstance (this);
The role of createinputinstance is mainly used to select the platform of course, different platforms will invoke different gesture input processing, there is a strategy to choose the processing of
functionCreateinputinstance (manager) {varType; varInputclass =Manager.options.inputClass; if(inputclass) {Type=Inputclass; } Else if(support_pointer_events) {Type=Pointereventinput; } Else if(Support_only_touch) {Type= TouchInput;//mobile phone side}Else if(!Support_touch) {Type= Mouseinput;//Desktop}Else{Type=Touchmouseinput; }}
If it's a desktop PC, we'll go mouseinput
return New Mouseinput (manager, Inputhandler);
This puts the specific callback through the input class binding to mouseinput handler processing, the final callback will go into the total processing Inputhandler class
The Inputhandler class iterates through all the gesture recognizers, passing input inputs
Manager.recognize (input);
Each recognizer deals with its behavior, of course, how it is triggered, and how the gesture recognizer determines the gesture, and puts a chapter in it.
Official: http://hammerjs.github.io/
my: Https://github.com/JsAaron/hammer-js
Analysis of Mobile development Framework (II) Hammer professional gesture control