Detecting and responding to a zoom gesture

How to

Detect and respond to a zoom gesture.

Solution

Set your input mode and add an event listener

// Enable dispatch of gesture
Multitouch.inputMode = MultitouchInputMode.GESTURE;

// Define gesture to listen for and name of function to dispatch when it occurs
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);

Create the function that is called to react to your event

public function onZoom(event:TransformGestureEvent):void
{
    // For this example code the currentScaleX and currentScaleY variables 
    // Are assumed to be Number variables in the root of the class
    // That keep track of total scale across events
	
    // Add event's scale change to total scale amount
    currentScaleX = currentScaleX * event.scaleX;
	currentScaleY = currentScaleY * event.scaleY;

    // Output results
    trace ("current scale is " +currentScaleX+ " X, " + currentScaleY + " Y");
    trace ("with a change of " +event.scaleX+ "X," + event.scaleY + "Y");
}

Build requirements

You must include the following classes in the class:

import flash.events.TransformGestureEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode; 

Discussion

To react to a zoom gesture, you should set the MultiTouchInputMode to GESTURE to dispatch a zoom gesture. After you set the input mode, add an event listener to handle a GESTURE_ZOOM event. To add functionality, you can evaluate the event.scaleX and event.scaleY values in the function to determine the scale change and react accordingly.