23 May 2011
Beginning
In this exercise you will use effects to animate components as they transition between states of an application.
In the Employee Portal example, Panel containers in the login and portal states will move and fade in and out as you switch states. You will also use composite effects to control which elements move in parallel or sequentially. Lastly, you will learn how to explicitly control the appearance and disappearance of components during the transition that exist in one state but not the other.
In this exercise, you will learn how to:
In this section you will animate the Employee of the Month Panel container as it transitions between two applications states.
states block.<s:states>
<s:State name="portalState"/>
<s:State name="loginState"/>
</s:states>
Remember that the Employee Portal application has two distinct states that you defined in earlier exercises. Next, you will define the animation that occurs on the Employee of the Month Panel container when the application transitions from the login to the portal state.
states tag, create the transitions property of the application.transitions block, create a Transition tag block.<s:transitions>
<s:Transition>
</s:Transition>
<s:transitions>
Transition tag, add the fromState property with a value of loginState and the toState property with a value of portalState.<s:Transition fromState="loginState" toState="portalState">
</s:Transition>
This is a one-way transition. You will create an animation for the reverse transition later in this exercise.
Transition tags, create a Parallel effect tag block.<s:Transition fromState="loginState" toState="portalState">
<s:Parallel>
</s:Parallel>
</s:Transition>
In the following steps you will apply both a Fade and a Move effect to the Employee of the Month Panel container. The effects will happen at the same time, which is why you are using a Parallel composite effect instead of a Sequence effect.
Parallel effect, assign the target property value to the employeeOfTheMonth Panel container.<s:Parallel target="{employeeOfTheMonth}">
</s:Parallel>
Parallel effect tags, add a Fade effect instance.<s:Parallel target="{employeeOfTheMonth}">
<s:Fade/>
</s:Parallel>
Fade effect, add a Move effect.<s:Parallel target="{employeeOfTheMonth}">
<s:Fade/>
<s:Move/>
</s:Parallel>
You should see the login Panel container disappear, the employeeOfTheMonth Panel container fade into view while moving into position, and the other Panel containers in the portalState just appear (see Figure 2).
In this section you will animate multiple containers so that they move into place from the locations shown in Figure 3.
Parallel effect, convert the target property to the targets property.<s:Parallel targets="{employeeOfTheMonth}">
...
</s:Parallel>
You want all the Panel instances to animate at the same time.
targets property value to the four Panel instances that appear in the portalState. Remember to add the array syntax square brackets inside the curly braces of the binding.<s:Parallel targets="{[employeeOfTheMonth, search,
cafeteriaSpecial, monthlyEvents]}">
...
</s:Parallel>
Move effect, assign the target property value to the employeeOfTheMonth Panel container.<s:Parallel targets="{[employeeOfTheMonth, search,
cafeteriaSpecial, monthlyEvents]}">
<s:Fade/>
<s:Move target="{employeeOfTheMonth}"/>
</s:Parallel>
You want all of the Panel instances to fade in except for the employeeOfTheMonth panel, which already appears in both states. By simply leaving the Fade effect without any properties, the application will automatically determine the alpha property of each target component and extrapolate from there how to make it fade from one state to the other.
Each Panel instance will move in a different way. Since the employeeOfTheMonth instance already has a starting point in the loginState of the application, you simply left the Move effect without properties. Again, the application will automatically determine how to animate the x and y properties of the employeeOfTheMonth instance to make it move to the proper location in the portalState from its initial position in the loginState.
The other three Panel container instances that are referenced in the targets property of the Parallel effect do not exist in the loginState and they currently just appear in their final position in the portalState. In the next steps, you will define a starting position for each of the instances using either the xFrom or yFrom properties of the Move effect.
Figure 3 above gives a practical illustration of where the Panel instances will start from when the animation begins.
Move effect for the employeeOfTheMonth instance, add another Move effect instance.Move effect, assign the target property value to the search Panel container and the xFrom property value to -166.<s:Move target="{employeeOfTheMonth}"/>
<s:Move target="{search}" xFrom="-166"/>
Move effect, add a third Move effect and assign the target property value to the cafeteriaSpecial Panel container and the yFrom property value to -329.<s:Move target="{search}" xFrom="-166"/>
<s:Move target="{cafeteriaSpecial}" yFrom="-329" />
Move effect, add a fourth Move effect and assign the target property value to the monthlyEvents Panel container and the xFrom property to 833.<s:Move target="{cafeteriaSpecial}" yFrom="-329" />
<s:Move target="{monthlyEvents}" xFrom="833" />
You should see the login panel disappear and the panels within the portal state move into place from their off-screen locations as shown in Figure 4.
In this section you will use a Sequence effect to define the order in which the panels will animate as they transition between the login and portal states. In the last section you had all the panels in the portal state fade and move into place in parallel. But before that happens, you need the login Panel instance to fade and move out of view.
Parallel effect with a Sequence effect tag block.<s:Sequence>
<s:Parallel target="{[employeeOfTheMonth, search, cafeteriaSpecial, monthlyEvents]}">
...
</s:Parallel>
</s:Sequence>
Sequence tag and the Parallel effect, create another Parallel effect instance.<s:Sequence>
<s:Parallel>
</s:Parallel>
<s:Parallel target="{[employeeOfTheMonth, search, cafeteriaSpecial, monthlyEvents]}">
...
</s:Parallel>
</s:Sequence>
Parallel effect tags, add a Fade effect and assign the target property value to the login Panel container.<s:Parallel>
<s:Fade target="{login}"/>
</s:Parallel>
You could have put the target property on the Parallel effect instead of on the nested Fade effect. However, we will next add more effects inside of this composite effect that target another component.
Fade effect, add a Move effect and assign the target property value to the login Panel container and add the xTo property with a value of -266.<s:Parallel>
<s:Fade target="{login}"/>
<s:Move target="{login}"
xTo="-266"/>
</s:Parallel>
Move effect, add another Move effect and assign the target property value to the employeeOfTheMonth Panel container.<s:Fade target="{login}"/>
<s:Move target="{login}"
xTo="-266"/>
<s:Move target="{employeeOfTheMonth}"/>
Parallel effect block, remove the employeeOfTheMonth Panel container from the targets property value and the corresponding Move effect.<s:Parallel targets="{[search, cafeteriaSpecial, monthlyEvents]}">
<s:Fade/>
<s:Move target="{search}" xFrom="-166"/>
<s:Move target="{cafeteriaSpecial}" yFrom="-329" />
<s:Move target="{monthlyEvents}" xFrom="833" />
</s:Parallel>
The changes you just made will fade and move the login panel out while the employeeOfTheMonth panel moves into its new location in the portalState. Only after that happens, will the second set of effects run, which animate the rest of the portalState panels into place.
You should see the animation is not correct. The Panel containers within the portalState state are visible in their final locations before the animations occur.
You have seen that the application can usually determine how to animate an effect on a component based on the beginning and ending properties that it is modifying. However, as an animation becomes more complex, the Flex framework needs more direction for how to animate elements that exist in only one state of the transition.
In this section, you will use the AddAction and RemoveAction effects to explicitly define when a component is added or removed during the transition between the two states.
Sequence tag, between the Parallel effects, add the RemoveAction tag with a target property bound to the login instance.<s:Parallel>
...
</s:Parallel>
<s:RemoveAction target="{login}"/>
<s:Parallel targets="{[search,
cafeteriaSpecial, monthlyEvents]}">
...
</s:Parallel>
You have just told the application to first animate the login and employeeOfTheMonth instances and then remove the login instance, which does not exist in the portal state.
Parallel tags, add the AddAction tag as the first effect.<s:Parallel targets="{[search,
cafeteriaSpecial, monthlyEvents]}">
<s:AddAction/>
<s:Fade/>
...
You have now told the application to explicitly add the panel instances to the transition at this point before you fade and move them.
You should see the Panel containers except for the Employee of the Month panel animate properly upon switching to the portalState state, as shown in Figure 5.
In this section you will use the Resize effect to change the size of the employeeOfTheMonth Panel container between states.
Parallel effect tag.Move effect, add the xFrom property with a value of 298 and the xTo property value of 24. These properties values represent the x position of the employeeOfTheMonth component in each application state.<s:Move target="{login}" xTo="-266"/>
<s:Move target="{employeeOfTheMonth}"
xFrom="298"
xTo="24"/>
Fade and the Move effects, add a Resize effect and assign the target property value to employeeOfTheMonth, the widthFrom property value to 390 and the widthTo property value to 250.<s:Fade target="{login}"/>
<s:Resize target="{employeeOfTheMonth}"
widthFrom="390"
widthTo="250"/>
<s:Move target="{login}"
xTo="-266"/>
<s:Move target="{employeeOfTheMonth}"
xFrom="298" xTo="24"/>
You should see the Employee of the Month panel animate before the other Panel containers within the portal state (see Figure 6).
Now you will add the autoReverse property to the transition so that your application animates when you transition from the portalState back to the loginState.
Transition tag, add the autoReverse property with a value of true.Notice that the transition reverses, however, it does not animate correctly. In the next section you will add a second transition from the portalState to the loginState.
Transition tag, remove the autoReverse property.In this section you will create a second transition to animate the components when the user logs out of the application. When this happens, you will animate from the portal state back to the login state.
transitions tags, below the existing Transition block, add another Transition tag block.<s:Transition fromState="loginState" toState="portalState">
...
</s:Transition>
<s:Transition>
</s:Transition>
Transition instance, add the fromState property with a value of portalState and the toState property with a value of loginState.<s:Transition fromState="portalState" toState="loginState">
</s:Transition>
When the user logs out of the Employee Portal application, you will fade out the search, cafeteriaSpecial, and monthlyEvents panels while the employeeOfTheMonth instance resizes and moves to its proper position in the login state. The next composite effect that you want to animate when that is done is for the Login panel to fade and move in from the left of the screen.
Transition effect, nest a Sequence effect.<s:Transition fromState="portalState" toState="loginState">
<s:Sequence>
</s:Sequence>
</s:Transition>
Sequence tags, add a Parallel effect to animate out the panels in the portalState.<s:Sequence>
<s:Parallel>
</s:Parallel>
</s:Sequence>
Parallel effect add a Fade effect and assign the targets property value to the three Panel containers that exist only in the portalState state.<s:Parallel>
<s:Fade targets="{[search, cafeteriaSpecial, monthlyEvents]}"/>
</s:Parallel>
Fade effect, add a Resize effect and assign the target property value to the employeeOfTheMonth Panel container, the widthFrom property value to 250 and the widthTo property value to 390.<s:Fade targets="{[search, cafeteriaSpecial, monthlyEvents]}"/>
<s:Resize target="{employeeOfTheMonth}" widthFrom="250" widthTo="390"/>
Resize effect, add the Move effect and assign the target property value to the employeeOfTheMonth Panel container, the xFrom property value to 24 and the xTo property value to 434.<s:Resize target="{employeeOfTheMonth}"
widthFrom="177" widthTo="264"/>
<s:Move target="{employeeOfTheMonth}"
xFrom="24" xTo="434"/>
Parallel composite effect, but still inside the Sequence effect, create a second set of Parallel tags that target the login instance.<s:Sequence>
<s:Parallel>
...
</s:Parallel>
<s:Parallel target="{login}">
</s:Parallel>
</s:Sequence>
Parallel effect, add a Fade effect.<s:Parallel target="{login}">
<s:Fade/>
</s:Parallel>
In the previous steps, you have defined the animations that should occur in sequence. First, all the panels in the portal state will fade, except the Employee of the Month panel, which will move to its new location. Then the Login panel will fade and move into place in the login state.
Lastly you need to define when the panels that do not exist in both states need to be added or removed during the transition.
Parallel tag blocks, add the RemoveAction tag and assign the targets property value to the Panel containers that exist only in the portalState state.<s:Sequence>
<s:Parallel>
...
</s:Parallel>
<s:RemoveAction targets="{[search, cafeteriaSpecial, monthlyEvents]}"/>
<s:Parallel target="{login}">
...
</s:Parallel>
</s:Sequence>
Parallel tag blocks, but below the RemoveAction effect, add an AddAction tag and assign the target property value to the login Panel container.<s:Sequence>
<s:Parallel>
...
</s:Parallel>
<s:RemoveAction targets="{[search, cafeteriaSpecial, monthlyEvents]}"/>
<s:AddAction target="{login}"/>
<s:Parallel target="{login}">
...
</s:Parallel>
</s:Sequence>
You should see that the Employee of the Month panel moves and resizes while the other panels fade away, and then the Login panel fades into view and moves into place as shown in Figure 7.
In this exercise you used effects to animate components as they transition between states of an application.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.