在精确的keyFrame上停止故事板

时间:2012-04-01 14:45:34

标签: wpf storyboard keyframe

我为我正在制作的一些游戏制作了一个游戏(在c#中),它是一个用户控件,它使用故事板来显示几个图像(如幻灯片),所以它看起来像一个滚动的3D模具。问题是在特定的keyFrame上启动和停止它。使用Pause()和Resume()似乎是合乎逻辑的,但我无法弄清楚如何在精确的keyFrame上暂停。

有些人使用单独的dispatcherTimer来执行此操作,但这并不足以将其停在该确切的关键帧上。 (例如,如果你掷4,它必须在4图像上停止。)

所以,如果有这样的方法会很棒:

TimeSpan keyTime = new TimeSpan(0,0,0,0,750); // 750 miliseconds
myStoryBoard.ResumeTo(keyTime); // <- doesn't exist as far as I know

以下是我在XAML中的故事板的片段:

<Storyboard x:Key="DieStoryBoard" RepeatBehavior="Forever">

        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image1">

            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

            <DiscreteObjectKeyFrame KeyTime="0:0:0.05">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Collapsed</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

        </ObjectAnimationUsingKeyFrames>


        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image2">

            <DiscreteObjectKeyFrame KeyTime="0:0:0.05">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

            <DiscreteObjectKeyFrame KeyTime="0:0:0.10">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Collapsed</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

        </ObjectAnimationUsingKeyFrames>


        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image3">

            <DiscreteObjectKeyFrame KeyTime="0:0:0.10">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

            <DiscreteObjectKeyFrame KeyTime="0:0:0.15">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Collapsed</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

        </ObjectAnimationUsingKeyFrames>
.....

有些图片可以让事情变得更清晰:

1 2 3 4

1 个答案:

答案 0 :(得分:2)

试试这个......

我的例子是一个旋转的箭头,我可以以指定的角度停止它。

<Window.Resources>
    <Storyboard x:Key="Storyboard1">
        <DoubleAnimationUsingKeyFrames     
            Storyboard.TargetProperty="(UIElement.RenderTransform).
            (TransformGroup.Children)[2].(RotateTransform.Angle)" 
            Storyboard.TargetName="RightPanelButton1">
            <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="45.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:2" Value="90.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:3" Value="135.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:4" Value="180.0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>



Storyboard st = ((Storyboard)this.Resources["Storyboard1"]);

st.Begin();
st.Seek(new TimeSpan(0,0,2));
st.Pause();

Abbas Ahmed