在你的移动Flex应用程序中添加滑动手势(swipe gestures)
随着Flex4.5 SDK(又名:Hero)的推出,你现在可以在Android设备上和BlackBerry PlayBook上创建一些非常漂亮的移动应用。这里面就有你可以用在触摸屏上的滑动手势。用这个手势,你可以在屏幕上与屏幕之间移动你想要的。但是,如何在你的Flex移动应用中实现这样的功能呢?
这里有两种方法你可以选择。
第一种涉及到使用ActionScript来附加一个事件侦听器。当你的视图组件初始化,你写入下的代码:
private function initView():void { addEventListener(TransformGestureEvent.GESTURE_SWIPE, onSwipe); }
复制代码这种方法的好处是,它在纯ActionScript项目中和在Flash Professional项目中用法都是一样的。
第二种方法只能用在Flex应用程序中。它实际上更容易实现一些。你只需要在View tag里捕获gestureSwipe事件就可以,如下所示:
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" gestureSwipe="onSwipe(event)"/>
这就是在有触摸屏的设备上滑动手势的处理,比如移动电话,平板电脑或者更大的触摸屏。但是,你只有一个单一的滑动事件,那么你怎么判断它一个向左的滑动还是一个向右的滑动呢?对此,你需要通过到这个事件的属性来确定滑动的方向,你需要水平方向的offsetX属性和垂直方向的offsetY属性,如下:
View Code
1 private function onSwipe(event:TransformGestureEvent):void { 2 3 // A swipe to the left means the offsetX property will be -1 4 // A swipe to the right means the offsetX position will be 1 5 if(event.offsetX == -1) { 6 doSwipeLeft(); 7 } else if(event.offsetX == 1) { 8 doSwipeRight(); 9 } 10 11 // The same principle applies to the vertical swipe gesture, so 12 // a swipe to the top means the offsetY position will be -1 13 // A swipe to the bottom means the offsetY position will be 1 14 if(event.offsetX == -1) { 15 doSwipeTop(); 16 } else if(event.offsetX == 1) { 17 doSwipeBottom(); 18 } 19 }