3

I want to know if there is a way to detect user is doing pinch zoom.

I want to do two things when user zooming in:

  1. let the div align the left side of screen
  2. set the width of div equal to the viewport width

I found Android has touchstart, touchend and touchmove, but they won't be triggered during multitouch. I want to complete above function right after finishing zooming.

Are there any other approaches?


I am not doing the web app. I am just adding some functions to the mobile version.

Community
  • 1
  • 1
Roger Chan
  • 31
  • 1
  • 3

2 Answers2

1

Android browser supports touchstart, touchend & touchmove with JavaScript, but the problem with older androids is the number of touches these events are detecting.

For example, this code will log the message on IOS and on newer android devices:

var obj = document.getElementById('elmId');
obj.addEventListener('touchmove', function(event) {
  if (event.targetTouches.length == 2) {
    console.log("exactly 2 fingers gesture inside elmId ");
  }
}, false);

Older androids will do nothing because event.targetTouches.length will never be equal to 2.

IMHO, you should use this approach that will support most devices and provide a fallback option for older devices (use other gestures for zooming like double tap or a button to zoom).

vsync
  • 118,978
  • 58
  • 307
  • 400
AssafDamari
  • 135
  • 1
  • 5
  • I have tried this method, but the function is triggered too early and only one time. The value I get isn't the latest. Here is the example. http://off.theorigo.com/~rogerchan/index.html – Roger Chan Mar 23 '12 at 01:32
0

Android has a ScaleGestureDetector. Here's an example. However, if you require additional multitouch support, e.g., rotation, I would recommend this library. I have used it personally (to store and retrieve transformation matrices of multiple images) and found it an excellent resource.

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43
  • 1
    Thanks for your answer. But the code on your example are java. How to add them into a normal html or javascript files? – Roger Chan Mar 22 '12 at 08:14
  • not entirely sure tbh. you could check this qn out : http://stackoverflow.com/questions/2727763/communication-between-android-java-and-phonegap-javascript – Dhruv Gairola Mar 22 '12 at 10:23