Share via


Cómo: Simplificar animaciones utilizando objetos Timeline secundarios

En este ejemplo se muestra cómo simplificar las animaciones mediante el uso de objetos secundarios ParallelTimeline. Un Storyboard es un tipo de Timeline que proporciona información de establecimiento de destino para las escalas de tiempo que contiene. Utilice un Storyboard para proporcionar información de establecimiento de destino de escalas de tiempo, incluida la información sobre la propiedad y el objeto.

Para comenzar una animación, use uno o más objetos ParallelTimeline como elementos secundarios anidados de un Storyboard. Estos objetos ParallelTimeline pueden contener otras animaciones y, por lo tanto, pueden encapsular mejor las secuencias de tiempo de animaciones complejas. Por ejemplo, si está animando un TextBlock y varias formas en un mismo Storyboard, puede separar las animaciones para el TextBlock y para las formas, poniendo cada uno en un ParallelTimeline distinto. Puesto que cada ParallelTimeline tiene su propio BeginTime y todos los elementos secundarios de ParallelTimeline comienzan en relación con este BeginTime, es mejor que el tiempo esté encapsulado.

En el ejemplo siguiente se animan dos bloques de texto (objetos TextBlock) desde el mismo Storyboard. Un ParallelTimeline encapsula las animaciones de uno de los objetos TextBlock.

Nota sobre el rendimiento: aunque se pueden anidar escalas de tiempo de Storyboard dentro de otras, los ParallelTimeline son más adecuados para el anidamiento porque requieren menos sobrecarga. (La clase Storyboard hereda de la clase ParallelTimeline).

Ejemplo

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas >

    <!-- TextBlock with text "ParallelTimelines are..." that gets animated. -->
    <TextBlock Name="FirstTextBlock" Canvas.Top="30" Canvas.Left="300" FontSize="24" >
      ParallelTimelines are...
      <TextBlock.RenderTransform>
        <TransformGroup>
          <SkewTransform x:Name="FirstTextBlockSkew" CenterX="25" CenterY="25" AngleX="0" AngleY="0" />
        </TransformGroup>
      </TextBlock.RenderTransform>
    </TextBlock>

    <!-- TextBlock with text "Useful" that gets animated. -->
    <TextBlock Name="SecondTextBlock" Opacity="0" Canvas.Top="30" Canvas.Left="585" FontSize="24" >
      Useful
      <TextBlock.RenderTransform>
        <TransformGroup>
          <SkewTransform x:Name="SecondTextBlockSkew" CenterX="25" CenterY="25" AngleX="0" AngleY="0" />
          <ScaleTransform x:Name="SecondTextBlockScale" CenterX="0" CenterY="24" />
        </TransformGroup>
      </TextBlock.RenderTransform>
    </TextBlock>

    <!-- Event Trigger that controls all animations on the page. -->
    <Canvas.Triggers>
      <EventTrigger RoutedEvent="Canvas.Loaded">
        <BeginStoryboard>
          <Storyboard>

            <!-- "ParallelTimelines are..." fades into view. -->
            <DoubleAnimation Storyboard.TargetName="FirstTextBlock"
            Storyboard.TargetProperty="Opacity" Duration="0:0:2" From="0" To="1" />

            <!-- "ParallelTimelines are..." skews to the left. -->
            <DoubleAnimation Storyboard.TargetName="FirstTextBlockSkew"
            Storyboard.TargetProperty="AngleX" Duration="0:0:1" BeginTime="0:0:2" From="0" To="45" />

            <!-- This ParallelTimeline contains all the animations for the TextBlock with the text
            "Useful" in it. This ParallelTimeline begins 4 seconds after the Storyboard timeline begins and all child
            animations begin relative to this parent timeline. -->
            <ParallelTimeline BeginTime="0:0:4">

              <!-- "Useful" fades into view. -->
              <DoubleAnimation Storyboard.TargetName="SecondTextBlock"
              Storyboard.TargetProperty="Opacity" Duration="0:0:2" From="0" To="1" />

              <!-- "Useful" slides in from the right. -->
              <DoubleAnimation Storyboard.TargetName="SecondTextBlockSkew"
              Storyboard.TargetProperty="AngleX" Duration="0:0:2" From="90" To="180" />

              <!-- "Useful" skews to the right. -->
              <DoubleAnimation Storyboard.TargetName="SecondTextBlockSkew"
              Storyboard.TargetProperty="AngleX" BeginTime="0:0:3" Duration="0:0:0.2" From="0" To="-60" />

              <!-- "Useful" Gets taller. -->
              <DoubleAnimation Storyboard.TargetName="SecondTextBlockScale"
              Storyboard.TargetProperty="ScaleY" BeginTime="0:0:3" Duration="0:0:0.2" From="1" To="3" />
            </ParallelTimeline>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </Canvas.Triggers>
  </Canvas>
</Page>

Vea también