2011年8月30日

Flex in Mobile(第五篇、Mobile Guesture使用方法)


Flex中要使用手勢也是非常簡單的,我們只要為我們的應用添加手勢的監聽器,然後在監聽器中進行項對應的處理即可完成,這與Android上的Guesture操作上來比較的話,的確簡單了許多,趕快來看怎麼實現吧。

<fx:Script>
        <!
[CDATA[
            protected function init
():void
            
{
                addEventListener
(TransformGestureEvent.GESTURE_SWIPE, swipeHandler);
            
}
            
            protected function swipeHandler
(event:TransformGestureEvent):void
            
{
                
if(event.offsetX == -1)
                
{
                    navigator.
pushView(views.Tour3);
                
}
                
else if(event.offsetX == 1) 
                
{

                    navigator.popView();
                
}
            
}
            
        
]]>
    </fx:Script>

使用方法非常簡單,只需要讓我們的應用監聽TransformGestureEvent.GESTURE_SWIPE事件,然後從event中取得offsetX 即可得知我們的手勢是向左或是向右做滑動囉。

Flex in Mobile(第四篇、Mobile觸碰螢幕操作方法)


首先創建一個新的Moblie專案,在views中添加新的view,命名為MultiTouchView
,然後在MXML中天下以下程式碼 :


<fx:Declarations>
        <fx
:Component className="Circle">
            <s
:Ellipse width="140" height="140">
                <s
:fill>
                    <s
:SolidColor color="blue" />
                </s
:fill>
            </s
:Ellipse>
        </fx
:Component>
</fx
:Declarations> 


Script區塊新增下面程式碼
<![CDATA[

            protected var circles:
Object = new Object();
            
            protected function init
():void
            
{

                Multitouch.
inputMode = MultitouchInputMode.TOUCH_POINT;
                

                
this.stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
                
this.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
                
this.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
            
}

            protected function onTouchBegin
(event:TouchEvent):void
            
{
                var c:Circle = new Circle
();
                c.
x = event.localX -70;
                c.
y = event.localY -70;
                addElement
(c);
                circles
[event.touchPointID] = c;
            
}
            

            protected function onTouchEnd
(event:TouchEvent):void
            
{
                
if (circles[event.touchPointID] != null)
                
{
                    removeElement
(circles[event.touchPointID]);
                    
delete circles[event.touchPointID];
                
}
            
}


            protected function onTouchMove
(event:TouchEvent):void
            
{
                
if (circles[event.touchPointID] != null)
                
{
                    circles
[event.touchPointID].x = event.localX -70;
                    circles
[event.touchPointID].y = event.localY - 70;
                
}
            
}
        
]]>

這段程式碼首先定義了一個自訂元件Circle,並使用橢圓形繪製,然後在程式碼區塊中宣告了它的引用,在初始化函式中我將螢幕的inputMode更改為MultitouchInputMode.TOUCH_POINT模式才能支援多點,並且讓我們的顯示螢幕(stage)監聽了三個事件,分別為觸碰開始、觸碰移動、觸碰結束,分別對這些事件使用不同方式處理。

完成!!
將它運行在你的實體機器上,就可以測試了!!

Flex in Mobile(第三篇、Mobile目錄架構解說)

接連上一篇[第一Mobile專案],我們打開專案可以看到三個檔案,分別為
    HelloFlexMobile
    HelloFlexMobileHomeView
    HelloFlexMobile-app.xml
接下來將會依依的說明這些檔案的用處以及他們的使用方法。



HelloFlexMobile
這個檔案定義了ViewNavigatorApplication它是整個程式的根結點,底下會有一個到N個的ViewNavigator





HelloFlexMobileHomeView
這個檔案也就是顯示在畫面上的View,一個Mobile程式會由一個到多個View所組成。






HelloFlexMobile-app.xml
這個檔案定義在一開始定義的那些設定,如果一開始所設定的不能滿足要求可以開啟這個檔案進行修改。

ShareThis