Stop storyboard at exact keyFrame

元气小坏坏 提交于 2019-12-07 08:15:12

问题


I made a die for some game I'm making (in c#), it's a usercontrol which uses a storyboard to show several images after each other (like a slideshow) so it looks like a rolling 3D die. The problem is starting and stopping it at a specific keyFrame. It's seems logical to use Pause() and Resume() for this, but I can't figure out how to Pause at an exact keyFrame.

Some people use a seperate dispatcherTimer to do this, but this isn't precise enough to stop it at that exact keyframe. (for example, if you throw 4 it must stop on the 4 image).

So, it would be great if there was some method like this:

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

Here is a snippet from my storyboard in 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>
.....

And some images to make things clearer:


回答1:


try this...

my example is a rotating arrow,and I can stop it at a specified angle.

<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


来源:https://stackoverflow.com/questions/9965262/stop-storyboard-at-exact-keyframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!