Mobile development Overview "WebView and Touch Events"

Source: Internet
Author: User

Android vs Ios

I have developed a page in the WebKit kernel Chrome, with the iphone and Andro to test, the legend of their browser kernel is also WebKit, then the question, why the same page in iOS and Android performance different, there are a variety of strange bugs ...

I try to find the fundamental difference between the two:

-IOS

With the launch of the iphone on June 29, 2007, WebKit entered the iphone OS platform and was customized by Apple to become the unique typesetting engine for the iphone OS platform;

-Android

In the old version of wirelessly:

People familiar with the Android system and HTML programming may have heard of an important class Android.webkit.WebView provided by Android, which inherits from the view class, which is similar to many other controls. The difference is that it can be used to render web pages. Currently, the implementation of WebView is based on the existing default WebKit kernel (Android default browser is built based on WebView), which differs from the WebKit kernel used by chromium, although they are called WebKit.

However, in the latest Android 4.4 KitKat version, the WebView implementation based on the Android WebKit was replaced by chromium-based WebView implementations.

-Conclusion

Thus, although they are called WebKit, but WebKit and WebKit are also different:

Read this article: [Developers need to know WebKit] you will learn that the same is WebKit it also has different ports, they focus on different parts, each WebKit port has a shared part, but also a large part of the function is not shared, including JS engine.

So I stopped and said, "Why is iOS all right with Android?" "Instead of starting to fix bugs ... Although sometimes these bugs are not so good to fix ...

What is WebView

As a front end, you must have heard the word webview. I have heard, also curious, put my web page on the phone to see the call WebView? What exactly is WebView?

Actually WebView is a class name or it's an API (in my opinion):

-In Android, this is a class Android.webkit.WebView inherited from the view class, used to lay out and render web pages;
-In iOS, this class is called UIWebView, which is also the UI interface for the application.

How can our front end be so familiar with the client's class name?

If you're a knowledgeable front-end then you must have heard of the hybrid app (mixed-mode mobile app). This refers to the app between Web-app and Native-app, which combines the advantages of a good user interaction experience with native app and the Web The benefits of app cross-platform development ".
In my opinion, this is what the front end does with iOS development and Android development. Its implementation is inseparable from the WebView, because the client needs to render the H5 page I provide, how to implement it? I don't know, either. Ask the client to develop it ...

Touch events

Now we know that mobile phone and mobile phone, WebKit and WebKit different, first know, I first look at the touch incident, and then see what the problem he will have.

Touch event:

1 Touchstart: Triggers when a finger is placed on the screen, 2 touchend: Triggers when a finger is removed from the screen, 3 touchmove: Triggers when a finger is already on the screen and moves on the screen; 4 Touchcancel: Triggered if too many fingers are on the screen or when a different action occurs.

Properties of the event object in the touch event:

1 touches: It contains a series of information about the current touch screen of each finger, 2 targettouches: like touches, but provides touch information for the current finger, 3 changedtouches: Contains information about the touch change of each finger.

How to understand it:
1. When you put down a finger, the above three attributes will provide the same information;
2. When you drop the second finger, the touches object contains two messages, one for each finger, and the targettouches only contains two messages when the second finger is placed on the same node as the first finger (otherwise it contains only the information of the second finger) The changedtouches will only contain information related to the second finger.
3. If almost two fingers touch the screen, there will be two finger information in the changedtouches.
4. If we move the finger on the screen, the only object that will change is changedtouches, which will contain information about one or two fingers we have moved;
5. If a finger is lifted, its information is removed from the touches and targettouches objects, but the information is found in changedtouches;
6. When we remove the last finger from the screen, the touches and targettouches objects will be empty, but Changedtouches will keep the last message with the finger.

The list of information for each finger contained in the touches object also has the following properties that we are familiar with in mouse events:

1 identifier-An identifier that is unique for each touch point (finger), 2 target-the DOM node of the current finger touch, and 3 the coordinates of the relative viewport (viewport) when the Clientx/clienty-touch event occurs ( Contains scrolling) 4 screenx/screeny-coordinates relative to the screen 5 pagex/pagey-coordinates relative to the entire document

[Simple Touch Event implementation drag-and-drop demo]

Touch event-related tip

1. Check whether the device is a touch screen device:
Usually we detect whether the device is a touchscreen with two methods, one through UA and one is feature detection:

var Istouch =!! Ua.match (/applewebkit.*mobile.*/) | | ' Ontouchstart ' in document.documentelement;

But I read a very good article two days ago. The judgment is no longer accurate. When the notebook is a touch screen notebook, the above method will still think he is a touch device.

The article gives the scheme: we can judge by the scheme given by the Interaction Media features.

In media query Levle 4, the group has added feature queries to the pointer category, and pointer has three conditional options: None, fine, and coarse:

1 None, the current device does not have any other than the keyboard or trackpad input mode 2 Fine, the current device uses the mouse to operate 3 coarse, indicating that the current device supports at least touch screen operation, may also support the mouse

With some combination, we can make a judgment:

Both touch screen and mouse Matchmedia ("(Pointer:coarse)"). Matches && Matchmedia ("(Pointer:fine)"). matches//Just touch screen Matchmedia ("(Pointer:coarse)"). Matches &&!matchmedia ("(Pointer:fine)"). matches//just Mouse!matchmedia ("(Pointer:coarse) "). Matches && Matchmedia (" (Pointer:fine) "). matches//does not have any kind of matchmedia (" (Pointer:none) "). Matches

Therefore, the more secure way to judge the touch screen should be this:

function Istouchscreen () {  if (window.matchmedia) {    //* *    * (!matchmedia ("(Pointer:coarse)"). Matches && Matchmedia ("(Pointer:fine)"). Matches) {    return false   }   //Firefox way:https:// developer.mozilla.org/en-us/docs/web/guide/css/media_queries#-moz-touch-enabled   if (MatchMedia ("- Moz-touch-enabled:0 "). Matches) {    return false   }}if (Window.ontouchstart! = null) {   return True  }}

Tap events in 2.zepto

[Zepto Touch Event Source]

Everyone on Earth knows that the touch events in the Zepto package are tied to the document. So the problem ensued.

Tap tap to penetrate the conditions that occur:
1. Two DOM nodes one in the previous;
2. Two nodes are the parent-child relationship;
3. The tap event is also bundled;
4. If the underlying element is an INPUT element, the input element is triggered to get the focus (which prevents bubbling and the default behavior of the browser may be different).

Workaround:
1. Use a library called Fastclick on GitHub;

2. Monitor the Touchend event and use Preventdefault () in the event to block bubbling;

3. Using the CSS3 pointer-events=true,pointer-events=none switch to achieve;

4. Delay a certain amount of time to handle the event.

5. Reasonable improvement of the DOM structure, whether the lower layer can use a tag as a link processing, the same is to try to improve the parent-child relationship, reasonable to avoid click through;

6. If it does not work, then use the Click to tap it;

Resources

[What developers need to know about WebKit]

[Understanding WebKit and Chromium: Android WebView based on the chromium kernel]

[Understanding Chromium WebView on WebKit and chromium:android 4.4]

[WebKit Baidu Encyclopedia]

[Chrom for Android you have to know about n things]

[JS at the Fingertips (1)]

[JS at the Fingertips (2)]

[JS at the Fingertips (3)]

[Android vs Iphone-touch Events]

[Touching and gesturing on iphone,android and more]

[Media query about interactive mode]

From:http://www.cnblogs.com/skylar/p/mobile-webview-touch.html

Mobile development Overview "WebView and Touch Events"

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.