Flash Multitouch Gesture Example
View Code
1 package com.justinimhoff.multitouch 2 { 3 import flash.events.MouseEvent; 4 import flash.events.TransformGestureEvent; 5 import flash.geom.Matrix; 6 import flash.geom.Point; 7 import flash.ui.Multitouch; 8 9 import mx.controls.Image; 10 11 public class GestureImage extends Image 12 { 13 private var offsetX:Number; 14 private var offsetY:Number; 15 16 public function GestureImage() 17 { 18 if(Multitouch.supportsGestureEvents){ 19 this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 20 this.addEventListener(MouseEvent.MOUSE_UP, stopDragging); 21 this.addEventListener(TransformGestureEvent.GESTURE_ROTATE, 22 onGestureRotate); 23 this.addEventListener(TransformGestureEvent.GESTURE_ZOOM, 24 onGesturePinch); 25 } 26 } 27 28 private function startDragging(event:MouseEvent):void 29 { 30 setAsCurrentChild(); 31 offsetX = event.stageX - this.x; 32 offsetY = event.stageY - this.y; 33 stage.addEventListener(MouseEvent.MOUSE_MOVE, moveImage); 34 } 35 36 private function stopDragging(event:MouseEvent):void 37 { 38 stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveImage); 39 } 40 41 private function moveImage(event:MouseEvent):void 42 { 43 this.x = event.stageX - offsetX; 44 this.y = event.stageY - offsetY; 45 event.updateAfterEvent(); 46 } 47 48 private function onGesturePinch(pinchEvent:TransformGestureEvent):void{ 49 setAsCurrentChild(); 50 var pinchMatrix:Matrix = this.transform.matrix; 51 var pinchPoint:Point = 52 pinchMatrix.transformPoint( 53 new Point((this.width/2), (this.height/2))); 54 pinchMatrix.translate(-pinchPoint.x, -pinchPoint.y); 55 pinchMatrix.scale(pinchEvent.scaleX, pinchEvent.scaleY); 56 pinchMatrix.translate(pinchPoint.x, pinchPoint.y); 57 this.transform.matrix = pinchMatrix; 58 } 59 60 private function onGestureRotate(rotateEvent:TransformGestureEvent):void 61 { 62 setAsCurrentChild(); 63 var rotateMatrix:Matrix = this.transform.matrix; 64 var rotatePoint:Point = 65 rotateMatrix.transformPoint( 66 new Point((this.width/2), (this.height/2))); 67 rotateMatrix.translate(-rotatePoint.x, -rotatePoint.y); 68 rotateMatrix.rotate(rotateEvent.rotation*(Math.PI/180)); 69 rotateMatrix.translate(rotatePoint.x, rotatePoint.y); 70 this.transform.matrix = rotateMatrix ; 71 72 } 73 74 private function setAsCurrentChild():void{ 75 this.parent.setChildIndex( this, this.parent.numChildren-1 ); 76 } 77 78 } 79 }