Problem classification
Swipe the screen to turn on the appropriate feature action.
Problem description
1, the user manually swipe the screen, according to the direction of the slide, open the corresponding function (such as: slide up the experience of the cash cow, slide to open the task details, left-swipe to open the cornucopia item viewing and other functions), sliding event capture problems.
2, we all know that there are angle problems, such as: to 330 degrees in the direction of sliding mobile phone, to calculate its direction.
3, HTML5 provides the sliding event, can only read to the start and end coordinates, calculate the angle problem.
4, mobile phone screen coordinates and standard coordinate system conversion problem.
Solution Solutions
1. Sliding screen events Use HTML5 's Touchstart to slide the start event and the Touchend slide end event.
2, the direction of the judgment, the starting point to do a plane coordinate system, and the end line to do straight lines, straight and x positive half axis calculation angle; We split the line at a 45-degree angle, such as: As long as the sliding angle is greater than or equal to 45 degrees and less than 135 degrees, it is determined to slide upward.
3. Use math.atan2 to calculate the line angle formed from the starting point and end point.
4, carefully contrast the standard coordinate system and the screen coordinate system, we found that the standard coordinate system, the upper half axis is negative, to achieve the conversion, only need to change the y-coordinate starting point and finally position.
The processing code is as follows:
- <script>
- //Return angle
- function getslideangle (dx, dy) {
- Returnmath.atan2 (dy, dx) * Math.PI;
- }
- return direction according to start and end 1: Up, 2: down, 3: Left, 4: Right, 0: not sliding
- function getslidedirection (StartX, Starty, EndX, EndY) {
- var dy = Starty-endy;
- var dx = endx-startx;
- Varresult = 0;
- //If the sliding distance is too short
- if (math.abs (dx) < 2 && math.abs (dy) < 2) {
- Returnresult;
- }
- Varangle = getslideangle (dx, dy);
- if (angle >= -45 && Angle < ) {
- result = 4;
- }Else if (angle >= && Angle < 135) {
- result = 1;
- }Else if (angle >= -135 && Angle < -45) {
- result = 2;
- }
- Else if ((Angle >= 135 && angle <= 180) | | (Angle >= -180 && Angle < -135)) {
- result = 3;
- }
- Returnresult;
- }
- //Sliding handling
- var startX, Starty;
- Document.addeventlistener (' touchstart ',function (ev) {
- StartX = ev.touches[0].pagex;
- Starty = ev.touches[0].pagey;
- }, false);
- Document.addeventlistener (' touchend ',function (ev) {
- var endx, EndY;
- EndX = ev.changedtouches[0].pagex;
- EndY = ev.changedtouches[0].pagey;
- var direction = getslidedirection (StartX, Starty, EndX, EndY);
- switch (direction) {
- Case 0:
- Alert ("no swipe");
- Break ;
- case 1:
- Alert ("up");
- Break ;
- Case 2:
- Alert ("down");
- Break ;
- Case 3:
- Alert ("left");
- Alert ("!");
- Break ;
- Case 4:
- Alert ("right");
- Break ;
- Default:
- }
- }, false);
- </script>
Reprinted: 33741859
Mobile app swipe screen direction to determine the solution, JS to determine gesture direction