Rock Mobile Docs

Please login to access all of the features of the Community Site.

Animations

Animations change how an element looks over a set amount of time, such as fading in a label or shaking a field. You define an animation once, then start it when the person does something or when the page loads.

There are two kinds. Triggered animations run when something starts them. Passive animations stay attached to an element and update it whenever a value they track changes. Each animation has its own page, listed below.

Defining Animations

Define an animation in the Resources of an ancestor. Animations are referenced by name, so each one needs an x:Key and has to be defined on an element above the one that uses it. You can’t define it on the element itself. In this layout, an animation for myLabel can go on the VerticalStackLayout or the ScrollView:

<ScrollView>
    <VerticalStackLayout>
        <Label x:Name="myLabel"
            Text="Hello Rock!" />
    </VerticalStackLayout>
</ScrollView>

Here it’s defined on the VerticalStackLayout:

<VerticalStackLayout>
    <VerticalStackLayout.Resources>
        <Rock:FadeToAnimation x:Key="FadeOut"
            Target="{x:Reference myLabel}"
            Duration="2000"
            Opacity="0" />
    </VerticalStackLayout.Resources>

    <Label x:Name="myLabel"
        Text="Hello Rock!" />
</VerticalStackLayout>

This animation targets myLabel and changes its Opacity from its current value to 0 over 2,000 milliseconds (2 seconds). Defining an animation doesn’t start it.

Starting Animations

Start an animation with a trigger or a behavior. A trigger starts it when the person does something, such as tapping a button. A behavior starts it automatically when the element loads.

With a Trigger

Add a BeginAnimation to an event trigger. When the person taps this button, the label fades out over 2 seconds:

<ScrollView>
    <VerticalStackLayout>
        <VerticalStackLayout.Resources>
            <Rock:FadeToAnimation x:Key="FadeOut"
                Target="{x:Reference myLabel}"
                Duration="2000"
                Opacity="0" />
        </VerticalStackLayout.Resources>

        <Label x:Name="myLabel"
            Text="Hello Rock!" />

        <Button Text="Fade">
            <Button.Triggers>
                <EventTrigger Event="Clicked">
                    <Rock:BeginAnimation Animation="{StaticResource FadeOut}" />
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </VerticalStackLayout>
</ScrollView>

With a Behavior

To start the animation when the page loads, add a BeginAnimationBehavior to the element instead:

<ScrollView>
    <VerticalStackLayout>
        <VerticalStackLayout.Resources>
            <Rock:FadeToAnimation x:Key="FadeOut"
                Target="{x:Reference myLabel}"
                Duration="2000"
                Opacity="0" />
        </VerticalStackLayout.Resources>

        <Label x:Name="myLabel"
            Text="Hello Rock!">
            <Label.Behaviors>
                <Rock:BeginAnimationBehavior Animation="{StaticResource FadeOut}" />
            </Label.Behaviors>
        </Label>
    </VerticalStackLayout>
</ScrollView>

A behavior starts when its element is attached to its parent, which can happen before the page is fully visible. To cover that gap, the animation waits 250 milliseconds before it begins.

Stopping Animations

Stop a running animation with an EndAnimation trigger. Here, one button starts the fade and the other stops it:

<ScrollView>
    <VerticalStackLayout>
        <VerticalStackLayout.Resources>
            <Rock:FadeToAnimation x:Key="FadeOut"
                Target="{x:Reference myLabel}"
                Duration="2000"
                Opacity="0" />
        </VerticalStackLayout.Resources>

        <Label x:Name="myLabel"
            Text="Hello Rock!" />

        <Button Text="Start Fade">
            <Button.Triggers>
                <EventTrigger Event="Clicked">
                    <Rock:BeginAnimation Animation="{StaticResource FadeOut}" />
                </EventTrigger>
            </Button.Triggers>
        </Button>

        <Button Text="Stop Fade">
            <Button.Triggers>
                <EventTrigger Event="Clicked">
                    <Rock:EndAnimation Animation="{StaticResource FadeOut}" />
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </VerticalStackLayout>
</ScrollView>

An EndAnimationBehavior also exists, but because behaviors run when the element loads, it’s rarely useful.

Triggered Animations

Triggered animations stay idle until something starts them, such as a tap on a button or a behavior when the page loads.

Passive Animations

Passive animations are always active. They track a value and update the element whenever that value changes. For example, a color can blend from one value to another as someone drags a slider.