Wpf:如何停止故事板动画?

时间:2014-03-26 11:55:47

标签: wpf animation

我有一个Image,我试图让用户能够从几个选项中更改图像的动画。用户可以选择新动画,但有时前一个选定的动画不会停止,我在Image上同时运行2个或更多个故事板。

我有这段代码:

            private void animateZoom1(object sender, EventArgs e)
        {
            effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, (FindResource("LogoStoryboard_Zoom") as Storyboard).Children[0] as DoubleAnimation);
        }


        private void animateZoom(Boolean mode)
        {
            if (mode)
            {
                effectSample.RenderTransform = new ScaleTransform();
                effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, (FindResource("LogoStoryboard_Zoom") as Storyboard).Children[0] as DoubleAnimation);
            }
            else
            {
                effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, null);
                cancelAnimation("LogoStoryboard_Zoom");
            }
        }

            private void animateZoom2(object sender, EventArgs e)
        {
            effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, (FindResource("LogoStoryboard_Zoom") as Storyboard).Children[1] as DoubleAnimation);
        }


        private void animateOpacity(Boolean mode)
        {
            if (mode)
            {
                (FindResource("LogoStoryboard_Opacity") as Storyboard).Begin(this, true);
            }
            else
            {
                cancelAnimation("LogoStoryboard_Opacity");
            }
        }

            private void animateScaleX2(object sender, EventArgs e)
        {
            effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, (FindResource("LogoStoryboard_ScaleX") as Storyboard).Children[1] as DoubleAnimation);
        }

        private void animateScaleX(object sender, EventArgs e)
        {
            effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, (FindResource("LogoStoryboard_ScaleX") as Storyboard).Children[0] as DoubleAnimation);
        }

        private void animateScaleX(Boolean mode)
        {
            if (mode)
            {
                effectSample.RenderTransform = new ScaleTransform();
                effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, (FindResource("LogoStoryboard_ScaleX") as Storyboard).Children[0] as DoubleAnimation);
            }
            else
            {
                effectSample.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, null);
                cancelAnimation("LogoStoryboard_ScaleX");
            }
        }


        private void cancelAnimation(String storyBoardName)
        {

            (FindResource(storyBoardName) as Storyboard).Stop(this);
            (FindResource(storyBoardName) as Storyboard).Remove(this);
        }

和这个

<Storyboard x:Key="LogoStoryboard_Zoom" RepeatBehavior="Forever">
        <DoubleAnimation
                Storyboard.TargetName="effectSample"
                Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleY)"
                By="0.5"
                Duration="0:0:0.5"
                BeginTime="0:0:0"
                Completed="animateZoom2" />
        <DoubleAnimation
                Storyboard.TargetName="effectSample"
                Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleY)"
                By="-0.5"
                Duration="0:0:0.5"
                BeginTime="0:0:0"
                Completed="animateZoom1" />
    </Storyboard>

<Storyboard x:Key="LogoStoryboard_Opacity" RepeatBehavior="Forever">
        <DoubleAnimation
                Storyboard.TargetName="effectSample"
                Storyboard.TargetProperty="Opacity"
                From="1"
                To="0"
                Duration="0:0:8"
                BeginTime="0:0:1"/>
        <DoubleAnimation
                Storyboard.TargetName="effectSample"
                Storyboard.TargetProperty="Opacity"
                From="0"
                To="1"
                Duration="0:0:8"
                BeginTime="0:0:10"/>
    </Storyboard>

<Storyboard x:Key="LogoStoryboard_ScaleX" RepeatBehavior="Forever">
        <DoubleAnimation 
            Storyboard.TargetName="effectSample"
            Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleX)"
            To="0.5"
            Duration="0:0:3"
            BeginTime="0:0:0"
            Completed="animateScaleX2"/>
        <DoubleAnimation 
            Storyboard.TargetName="effectSample"
            Storyboard.TargetProperty="(Image.RenderTransform).(ScaleTransform.ScaleX)"
            To="1.5"
            Duration="0:0:3"
            BeginTime="0:0:0"
            Completed="animateScaleX"/>
    </Storyboard>

请帮我停止旧故事板:)

0 个答案:

没有答案