官术网_书友最值得收藏!

Using gestures to swipe a display object

Swipe is one of the most common gestures on Android devices, and with good reason. Whether flipping through a series of photographs, or simply moving between states in an application, the swipe gesture is something users have come to expect. A swipe gesture is accomplished by simply touching the screen and swiping up, down, left, or right across the screen quickly in the opposite direction.

How to do it...

This example draws a square within a Shape object using the Graphics API, adds it to the Stage, and then sets up a listener for swipe gesture events in order to move the Shape instance against the bounds of our screen in accordance with the direction of swipe:

  1. First, import the following classes into your project:
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.TransformGestureEvent;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;
    
  2. Declare a Shape object which we will perform the gestures upon:
    private var box:Shape;
    
  3. Next, construct a method to handle the creation of our Shape and add it to the DisplayList:
    protected function setupBox():void {
    box = new Shape();
    box.graphics.beginFill(0xFFFFFF, 1);
    box.x = stage.stageWidth/2;
    box.y = stage.stageHeight/2;
    box.graphics.drawRect(-150,-150,300,300);
    box.graphics.endFill();
    addChild(box);
    }
    
  4. Set the specific input mode for the multitouch APIs to support touch input by setting Multitouch.inputMode to the MultitouchInputMode.TOUCH_POINT constant and register an event listener for TransformGestureEvent.GESTURE_SWIPE events:
    protected function setupTouchEvents():void {
    Multitouch.inputMode = MultitouchInputMode.GESTURE;
    stage.addEventListener(TransformGestureEvent. GESTURE_SWIPE, onSwipe);
    }
    
  5. We can now respond to the data being returned by our swipe event. In this case, we are simply shifting the x and y position of our Shape based upon the swipe offset data:
    protected function onSwipe(e:TransformGestureEvent):void {
    switch(e.offsetX){
    case 1:{
    box.x = stage.stageWidth - (box.width/2);
    break;
    }
    case -1:{
    box.x = box.width/2;
    break;
    }
    }
    switch(e.offsetY){
    case 1:{
    box.y = stage.stageHeight - (box.height/2);
    break;
    }
    case -1:{
    box.y = box.height/2;
    break;
    }
    }
    }
    
  6. The resulting gesture will affect our visual object in the following way:

Note

Illustrations provided by Gestureworks (www.gestureworks.com).

How it works...

As we are setting our Multitouch.inputMode to gestures through MultitouchInputMode.GESTURE, we are able to listen for and react to a host of predefined gestures. In this example we are listening for the TransformGestureEvent.GESTURE_SWIPE event in order to shift the x and y position of our Shape object. By adjusting the coordinates of our Shape through the reported offset data, we can adjust the position of our object in a way that the user expects.

We can see through this example that the offsetX and offsetY values returned by our event listener will each either be 1 or -1. This makes it very simple for us to determine which direction the swipe has registered:

  • Swipe up: offsetY = -1
  • Swipe down: offsetY = 1
  • Swipe left: offsetX = -1
  • Swipe right: offsetX = 1

There's more...

When reacting to swipe events, it may be a good idea to provide a bit of transition animation, either by using built in tweening mechanisms, or an external tweening engine. There are many great tweening engines for ActionScript freely available as open source software. The use of these engines along with certain gestures can provide a more pleasant experience for the user of your applications.

We might consider the following popular tweening engines for use in our application:

TweenLite: http://www.greensock.com/tweenlite/

GTween: http://www.gskinner.com/libraries/gtween/

See also...

TransformGestureEvent.GESTURE_SWIPE is just one of a set of four primary transform gestures available to us when working with the Flash Platform runtimes and Android devices. Reference the following recipes for a complete overview of these gestures:

  • Using gestures to zoom a display object
  • Using gestures to pan a display object
  • Using gestures to rotate a display object
主站蜘蛛池模板: 麻阳| 阿拉善盟| 泽州县| 双桥区| 龙川县| 岳阳市| 诸暨市| 东辽县| 娄烦县| 马龙县| 高唐县| 香港| 朝阳市| 仙居县| 阿坝县| 冀州市| 武城县| 华池县| 郧西县| 香格里拉县| 浏阳市| 武威市| 铅山县| 澄城县| 灵丘县| 万荣县| 莱州市| 老河口市| 上林县| 内丘县| 富阳市| 东山县| 岱山县| 五原县| 卓资县| 出国| 康乐县| 侯马市| 敦煌市| 青岛市| 喀什市|