动态地改变一个旋转动画在WPF [英] Dynamically Change a Rotation Animation in WPF

查看:181
本文介绍了动态地改变一个旋转动画在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是DoubleAnimation是来anamiate一个RotationTransform的Angle属性。每秒几次,我需要改变旋转速率响应于外部数据,使得旋转随时间加快和/或减慢(平滑)。我目前通过使用重复永远从0.0到360.0持续时间为X中的DoubleAnimation是这样,那么每秒钟数次:

I am using a DoubleAnimation to anamiate the Angle property of a RotationTransform. Several times per second, I need to change the rate of the rotation in response to external data so that the rotation speeds up and/or slows down (smoothly) over time. I am currently doing this by using a DoubleAnimation that repeats forever from 0.0 to 360.0 with duration X, then several times per second:


  • 抓斗从外部数据的新值

  • 修改对DoubleAnimation是率,以该值

  • 再次重新应用到DoubleAnimation是Angle属性

请注意:我没有发现我不得不改变要和从属性上的当前角度和当前角度+ 360的动画 - 对我来说幸运RotationTransform与角度> 360度的任何麻烦 - 到prevent从零角度重新开始转动。

Note: I did find that I had to change the To and From properties on the animation to "current angle" and "current angle+360" - lucky for me RotationTransform has no trouble with angles > 360 degrees - to prevent starting the rotation over again from zero angle.

我的问题是:这是合理的?这似乎并非如此。在旋转Continously应用新DoubleAnimations的角度属性Transform似乎是错误的 - 有点像我让WPF动画旋转,而 I 的动画我的转速自己

My question is: Is this reasonable? It does not seem so. Continously applying new DoubleAnimations to the Angle property on a rotation transform seems wrong - sort of like I am letting WPF animate the rotation, while I am animating the rotation speed myself.

有没有更好的办法?

推荐答案

在故事板有一个Speed​​Ratio设置这是一个乘数的持续时间。你不能绑定到然而,这因为它不是一个依赖项属性。

On the storyboard there is a SpeedRatio setting which is a multiplier to the duration. You cannot bind to this however as it is not a dependency property.

要解决这个问题,你可以使用在故事板SetSpeed​​Ratio功能。请注意,这只是工作,如果故事板是code启动(其他明智的你得到一个错误)。

To get around this you can use the SetSpeedRatio function on the storyboard. Note this only works if the story board is started in code (other wise you get an error).

下code是的,你将如何在一个对象募集事件影响的旋转矩形的动画的速度一个充满例子。文本框和数据绑定的目的是要更新的背景对象。该按钮就是这么文本框失去焦点并更新对象。

The code below is an full example of how you would raise event in an object to effect the speed of the animation of a spinning rectangle. The purpose of the textbox and data bindings are to update the background object. The button is just so the textbox looses focus and updates the object.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
      <Rectangle Margin="50" Width="50" Height="50" Fill="Red" x:Name="rc">
        <Rectangle.RenderTransform>
          <RotateTransform x:Name="TransRotate"
                           CenterX="25" CenterY="25" Angle="0" />
        </Rectangle.RenderTransform>
        <Rectangle.Resources>
          <Storyboard x:Key="spin">
            <DoubleAnimation x:Name="da" 
                             Storyboard.TargetName="TransRotate" 
                             Storyboard.TargetProperty="Angle"
                             By="360" 
                             Duration="0:0:10"  
                             AutoReverse="False" 
                             RepeatBehavior="Forever" />
          </Storyboard>
        </Rectangle.Resources>
      </Rectangle>
      <TextBox Text="{Binding Speed}" />
      <Button>Update Speed</Button>
    </StackPanel>
</Window>

那么C#code

Then the C# code

{
    public Window1()
    {
        InitializeComponent();

        //create new  object
        BackgroundObject bo = new BackgroundObject();

        //binding only needed for the text box to change speed value
        this.DataContext = bo;

        //Hook up event
        bo.SpeedChanged += bo_SpeedChanged;

        //Needed to prevent an error
        Storyboard sb = (Storyboard)rc.FindResource("spin");
        sb.Begin(); 
    }

    //Change Speed
    public void bo_SpeedChanged(  object sender, int newSpeed)
    {
        Storyboard sb = (Storyboard)rc.FindResource("spin");
        sb.SetSpeedRatio(newSpeed);
    }
}

public delegate void SpeedChangedEventHandler(object sender, int newSpeed);

public class BackgroundObject
{
    public BackgroundObject()
    {
        _speed = 10;
    }

    public event SpeedChangedEventHandler SpeedChanged;

    private int _speed;
    public int Speed
    { 
        get { return _speed; }
        set { _speed = value; SpeedChanged(this,value); }
    }
}

我相信你一定可以适应您的使用。

I am sure you can adapt to your usage.

这篇关于动态地改变一个旋转动画在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆