This is Part 5 of a multipart series of articles that cover tips and tricks for Flex mobile development. Earlier parts focused on:

This article covers transitions when switching between views and when switching between tabs.

Using view transitions

While navigating through a Flex mobile application from view to view, by default one view slides off the screen as the new one slides on. You can change the animation or effect used in this view transition using one of four different classes that are available in the spark.transitions package in Flex 4.5:

  • SlideViewTransition
  • CrossFadeViewTransition
  • FlipViewTransition
  • ZoomViewTransition

Each of these transition classes can be applied with their default properties, or you can set the direction, duration, and mode (for some) to create a different effect. The following code snippets illustrate how each of these classes can be applied:

var flipTrans:FlipViewTransition = new FlipViewTransition(); flipTrans.direction = ViewTransitionDirection.UP; flipTrans.mode = FlipViewTransitionMode.CUBE; //or CARD mode navigator.pushView(SampleZoom,null,null,flipTrans); var zoomTrans:ZoomViewTransition = new ZoomViewTransition(); zoomTrans.direction = ViewTransitionDirection.RIGHT; zoomTrans.mode = ZoomViewTransitionMode.IN; //or OUT mode navigator.popToFirstView(zoomTrans); var slideTrans:SlideViewTransition = new SlideViewTransition(); slideTrans.direction = ViewTransitionDirection.DOWN; slideTrans.mode = SlideViewTransitionMode.UNCOVER; //or COVER and PUSH modes navigator.pushView(SampleZoom,null,null,slideTrans); var fadeTrans:CrossFadeViewTransition = new CrossFadeViewTransition(); fadeTrans.direction = ViewTransitionDirection.LEFT; // no modes are available for CrossFadeViewTransition navigator.pushView(SampleZoom,null,null,fadeTrans);

By default SlideViewTransition (with PUSH mode and LEFT direction) is used for the pushing and popping of all views. You can change the default transition used when a new view is pushed onto the stack by settingnavigator.defaultPushTransition . Likewise, you can setnavigator.defaultPopTransition to change the default transition used when the view is popped off the view stack (so the view beneath it is shown) . In either case, you set the property to an instance of a transition class; for example:

navigator.defaultPushTransition = new FlipViewTransition(); navigator.defaultPopTransition = new FadeViewTransition();

You can set these properties on any navigator within the main ViewNavigatorApplication or TabbedViewNavigatorApplication. You can also set them on the View class itself. However, if you want the change to apply to the entire application, set it in the root application code.

To see how this works, download the sample files for this article and import ViewTransitionsSample.fxp into Flash Builder. The application (see Figure 1) demonstrates the transitions and shows how to use the different modes that are available. For example, with FlipViewTransition you can see the difference betweenCARD and CUBE mode. You can also test how applying an easing function such as Bounce or Elasticmight affect how it plays. Fun stuff!

Figure 1. The view transitions sample app

Below is a short video clip of the application running on an iPod Touch. It's nothing fancy, but you can see the different effects played when the four different view transition types are used.

All of the ViewTransition classes extend ViewTransitionBase. The ViewTransitionBase class (and all that extend it) will dispatch FlexEvent.TRANSITION_START and FlexEvent.TRANSITION_END events when the transition starts and ends respectively. You can create your own custom transitions by extending ViewTransitionBase. If you plan to do so though, you should first review the Flex 4.5 ViewTransition specification. Note that this is the original specification and some names of properties, events, and other details have changed.

Implementing tab transitions

The ViewTransition classes are great for applying an effect to your view navigation within a ViewNavigator. However, you cannot use the ViewTransition classes when switching between tabs, because you are no longer operating within the same ViewNavigator, but rather between ViewNavigator containers. It is, however, still possible to apply similar effects to those provided by ViewTransition, and I've outlined one approach below.

First, you need to add an IndexChangeEvent handler to the implicit tabbedNavigator property in your TabbedViewNavigatorApplication on applicationComplete ; for example:

protected function tabbedviewnavigatorapplication1_applicationCompleteHandler(event:FlexEvent):void { this.tabbedNavigator.addEventListener(IndexChangeEvent.CHANGE,onChange); }

The IndexChangeEvent.CHANGE event is dispatched when a tab is selected, so you can perform any necessary handling within the handler. In this case, you want to apply a Spark Effect to the View that is being selected. You can check the newIndex property on the IndexChangeEvent and play different effects for each tab if you want.

You'll need to set up some effects to use. For example, you can define the following in the root application MXML:

<fx:Declarations> <s:Sequence id="seqEffect"> <s:Parallel> <s:Scale duration="800" id="scaleUp" scaleXBy=".8"/> <s:Rotate3D angleYFrom="0.0" angleYTo="360" duration="1600" repeatCount="{2}" repeatBehavior="reverse"/> </s:Parallel> <s:Scale duration="300" id="scaleDown" scaleXBy="-.8"/> </s:Sequence> <s:Move3D id="moveEffect" duration="300" xFrom="400" xTo="0"/> <s:Fade duration="800" id="fadeEffect" alphaFrom="0" alphaTo="1.0"/> </fx:Declarations>

Note: If you're not familiar with Spark Effects, check out Tour de Flex under Flex 4 > Components > Effects to see examples of how to use them all.

In the onChange handler, simply check the event index and set the target of the effect to the matching ViewNavigator container's activeView property:

protected function onChange(event:IndexChangeEvent):void { if (event.newIndex == 0) seqEffect.play([v1.activeView]); else if (event.newIndex == 1) moveEffect.play([v2.activeView]); else if (event.newIndex == 2) fadeEffect.play([v3.activeView]); }

Since you're handling the IndexChangeEvent.CHANGE event and not theIndexChangeEvent.CHANGING event, you can be sure that activeView has been set to the ViewNavigator container's new active view.

When the effects are played you will see a transition of sorts similar to what you might see when using a ViewTransition. You can see that you could also create more complex transitions using a sequence or parallel set of effects, and even add an easing function if desired. To simply mimic the ViewTransition classes, you would likely use the Spark Fade, Move, Rotate3D, and Scale effects alone to replicate the CrossFadeViewTransition, SlideViewTransition, FlipViewTransition, and ZoomViewTransition classes respectively. The code above will cause the effect to apply to the View itself, similar to how the ViewTransition classes work by default. However, you could also have the effect include the action bar in the animation as well by changing the code as follows:

protected function onChange(event:IndexChangeEvent):void { if (event.newIndex == 0) seqEffect.play([v1]); else if (event.newIndex == 1) moveEffect.play([v2]); else if (event.newIndex == 2) fadeEffect.play([v3]); }

Here is the full source for the main TabbedViewNavigatorApplication class:

<?xml version="1.0" encoding="utf-8"?> <s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" applicationComplete="tabbedviewnavigatorapplication1_applicationCompleteHandler(event)"> <fx:Style source="styles.css"/> <fx:Script> <![CDATA[ import mx.events.FlexEvent; import spark.events.IndexChangeEvent; import views.SlideView; protected function tabbedviewnavigatorapplication1_applicationCompleteHandler(event:FlexEvent):void { this.tabbedNavigator.addEventListener(IndexChangeEvent.CHANGE,onChange); } protected function onChange(event:IndexChangeEvent):void { if (event.newIndex == 0) seqEffect.play([v1.activeView]); else if (event.newIndex == 1) moveEffect.play([v2.activeView]); else if (event.newIndex == 2) fadeEffect.play([v3.activeView]); } ]]> </fx:Script> <fx:Declarations> <s:Sequence id="seqEffect"> <s:Parallel> <s:Scale duration="800" id="scaleUp" scaleXBy=".8"/> <s:Rotate3D angleYFrom="0.0" angleYTo="360" duration="1600" repeatCount="{2}" repeatBehavior="reverse"/> </s:Parallel> <s:Scale duration="300" id="scaleDown" scaleXBy="-.8"/> </s:Sequence> <s:Move3D id="moveEffect" duration="300" xFrom="400" xTo="0"/> <s:Fade duration="800" id="fadeEffect" alphaFrom="0" alphaTo="1.0"/> </fx:Declarations> <s:ViewNavigator id="v1" label="Welcome" width="100%" height="100%" firstView="views.WelcomeView" activate="seqEffect.play([v1.activeView])"/> <s:ViewNavigator id="v2" label="Slide View" width="100%" height="100%" firstView="views.SlideView" /> <s:ViewNavigator id="v3" label="Fade View" width="100%" height="100%" firstView="views.FadeInView" /> </s:TabbedViewNavigatorApplication>

If you want to play an initial effect when the application is run on the first view, you can also add anactivate event handler on the ViewNavigator and play the effect on the active view in the same manner, such as:

<s:ViewNavigator id="v1" label="Welcome" width="100%" height="100%" firstView="views.WelcomeView" activate="effect1.play([v1.activeView])"/>

Here's a short, 28-second video of this running on my iPhone 4:

The full source code for the sample application (see Figure 2) is available with the sample files for this article.

Figure 2. The tab transitions sample app

Flex移动应用程序开发的技巧和窍门(五)的更多相关文章

  1. Flex移动应用程序开发的技巧和窍门(三)

    这是关于 Flex 移动应用程序开发的技巧和窍门系列文章的第三部分内容.第一部分内容主要集中讨论了视图之间以及应用程序执行之间切换时的数据处理.第二部分则主要涵盖了应用程序动作条和标签组件风格化方面的 ...

  2. Flex移动应用程序开发的技巧和窍门(二)

    范例文件 flex-mobile-dev-tips-tricks-pt2.zip 这是关于Flex移动应用程序开发的技巧和窍门的一系列文章中的第二部分.第一部分 内容主要集中讨论了视图之间以及应用程序 ...

  3. Flex移动应用程序开发的技巧和窍门(一)

    这是一个由多个部分组成的系列文章的第一部分,它包含了Flex移动开发的若干技巧.如果你过去习惯于桌面和Web编程,你会发现,开发移动应用程序将面临一系列新的挑战. 除了重新思考你的对数据存储和处理的策 ...

  4. Flex移动应用程序开发的技巧和窍门(四)

    范例文件 flex-mobile-dev-tips-tricks-pt4.zip 这是本系列文章的第四部分,该系列文章涵盖Flex移动开发的秘诀与窍门. 第一部分关注切换视图以及切换执行应用时的数据处 ...

  5. 微信小程序开发调试技巧

    1.  查看线上小程序console a.  先打开开发小程序console b.  再打开线上小程序,此时可以查看console

  6. 微信小程序开发小技巧——单击事件传参、动态修改样式、轮播样式修改等

    一. 脚本部分: 1. 表达式无效的处理: 如果你发现自己编写的表达式无效或者数据不展示,那么请先检查你的表达式是否有添加{{}},小程序中全部都要添加的,只要是在模板中调用js中的数据 2. 获取元 ...

  7. 微信小程序开发小技巧:

    小技巧:输入view.tabs_content就可以生成下面的代码. 输入p10,就可以得到: 输入jc:c得到:文字水平对齐 输入d:f得到: 输入ai:c得到: 输入bb得到: currentCo ...

  8. flex开发小技巧集锦

    关于flex开发网上有非常多的相关信息介绍,因此我们要想学习关于flex开发的知识信息技能是一件非常简单和方便的事情.而针对于flex开发小编要告诉大家的是一些flex开发小技巧.利用这些小技巧能够有 ...

  9. 【Abode Air程序开发】Flex air文件打包和运行

    1 安装Adobe AIR 运行时,和java的JVM类似. Adobe AIR 运行时允许在桌面运行AIR应用程序,脱离游览器的束缚. 下载安装文件http://get.adobe.com/cn/a ...

随机推荐

  1. 关于Bean\Entity\Model\POJO的一些个人理解

    本文没有长篇累牍的,严格的,标准的表述,只是我在开发过程中,读书过程中的一些个人理解,可能不太准备,但是我觉得应该是最方便初学者理解的吧? 一.Bean 对于Bean而言,我的理解是只要是Java的类 ...

  2. win7 以管理员身份运行cmd, windows services 的创建和删除

    以 http 协议访问svn repository 搭建可用http访问的svn(windows) http://blog.csdn.net/yangyangrenren/article/detail ...

  3. Android编程中的实用快捷键

    作为一个优秀的程序员,不但要能开发出漂亮的软件,也要能熟练掌握编程的技巧,包括IDE的快捷键使用.比如linux 下的VI编辑器,对于不熟练快捷键的人来说就是一个噩梦,但一旦你熟练了VI的快捷键,VI ...

  4. 【翻译】go语言中的map实战

    业余时间翻译,水平很差,如有瑕疵,纯属无能. 原文链接 http://blog.golang.org/go-maps-in-action go语言中的map实战 1. 简介 哈希表是计算机科学中最重要 ...

  5. Cocos2dx 3.1.1 将一个2.X的项目改成3.1版本

    最近在论坛上下载到了一个Cocos2dx的单机跑酷例子, 也不知道是2.x版的, 花了一天时间试着把他改成3.1.1的试试, 现在已经可以顺利编译的, 但是还是有Heap Free的问题,调试了好几天 ...

  6. Linux启动时显示Grub命令行及修改

    1.在启动Linux系统时,如果/boot/grub/grub.cfg文件损坏或者不存在时,启动Linux时,就会有Grub命令行的提示. 如下操作,将系统自带的grub.cfg文件改名.重新启动系统 ...

  7. Codeforces AIM Tech Round3

    打得最烂一场Codeforces,多次都错题,无限WA... A题: 题意:给定n个橘子的大小,大小超过b的丢掉,不足d的补充进来,同时超过d的部分去掉,问要去掉几次 分析:直接模拟即可 #inclu ...

  8. 关于Discuz与jQuery冲突问题的亲测解决方法

    最近的一个项目整合dede和discuz程序,客户要求风格统一,所以有很多样式及特效都是要公用的.其中jQuery库定义的函数$()正好与discuz的comme.js中函数一样,这样就冲突了,导致d ...

  9. python处理时间--- datetime模块

    1   Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...

  10. jquery判断对象的type

    利用Object.toString.call()方法 看代码 先初始化class2type,将每种对象的toString后的值和type建立对应关系 core_toString.call([])输出& ...