首页 文章

多个框架的故事板

提问于
浏览
1

我想为三个帧的不透明度设置动画,并且它们的不透明度值的时间轴应该在图像中看起来像 . 动画应该是永远的,但我也想在每个周期之间延迟 . 我的意思是不透明度应该保持在指定时间内的最后一个值“1”,然后时间线应该再次重复 . 我尝试使用带有三个双动画的故事板,但我无法弄清楚每个周期之间的延迟和每个动画之间的延迟 . 设置每个动画的开始时间对我来说不起作用 .

Timeline

1 回答

  • 1

    由于WPF中动画的灵活性,有很多方法可以解决这个问题 . 在这里,我介绍最直观的解决方案 . 从您的图像中,您必须使用某种离散的KeyFrame . 在这种情况下,我们需要3 DoubleAnimationUsingKeyFrames ,每个应该有2 DiscreteDoubleKeyFrame . 所有这些关键帧应该是相同的 . 我们只需要适当地设置 BeginTimeDoubleAnimationUsingKeyFrames . 我们不需要设置 Duration ,它将根据 KeyTimeDiscreteDoubleKeyFrame )和 BeginTimeDoubleAnimationUsingKeyFrames )结合自动延迟 . 但是,如果您希望在循环之间有一些延迟,我们必须将 Duration 设置为最后一个 DoubleAnimationUsingKeyFrames .

    这是一个简单的例子:

    <StackPanel>
       <StackPanel.Triggers>
           <EventTrigger RoutedEvent="Loaded">
              <BeginStoryboard>
                  <Storyboard Storyboard.TargetProperty="Opacity"  
                              RepeatBehavior="Forever">
                     <DoubleAnimationUsingKeyFrames Storyboard.TargetName="i1"
                                                    BeginTime="0:0:0">
                        <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                        <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                     </DoubleAnimationUsingKeyFrames>
                     <DoubleAnimationUsingKeyFrames Storyboard.TargetName="i2" 
                                                    BeginTime="0:0:1">
                        <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                        <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                     </DoubleAnimationUsingKeyFrames>
                     <DoubleAnimationUsingKeyFrames Storyboard.TargetName="i3" 
                                                BeginTime="0:0:2" Duration="0:0:3">
                        <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                        <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                     </DoubleAnimationUsingKeyFrames>
                  </Storyboard>
              </BeginStoryboard>
           </EventTrigger>
       </StackPanel.Triggers>
       <TextBlock Name="i1">Item 1</TextBlock>
       <TextBlock Name="i2">Item 2</TextBlock>
       <TextBlock Name="i3">Item 3</TextBlock>
    </StackPanel>
    

    为简单起见,我在此代码中使用了 TextBlock . 在这里你可以看到 0 不透明度(对于每个 TextBlock )的时间是1秒 . 从那里你可以为每个 DoubleAnimationUsingKeyFrames 正确派生 BeginTime . 最后一个具有 Duration3 秒,这意味着延迟大约是 2 秒(最后一个动画的1秒从总数3中扣除) .

相关问题