【问题标题】:WPF rotate animationWPF旋转动画
【发布时间】:2010-11-15 23:05:01
【问题描述】:

我正在使用 WPF,但无法实现动画。

我有一个从 x 度旋转的矩形,其渲染变换原点从 0,0 开始。 我希望这个矩形在 2 秒后从 y 度旋转,渲染变换原点从 0,1 开始。

我当然想为第二个动画保留矩形位置。

我的问题是,当我更改渲染变换原点并应用第二次旋转时,当前位置没有保留,他从初始位置移动。

知道如何实现这一目标吗?

感谢您的帮助。

<Window x:Class="SimpleMove"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OpenWWindow" Height="900" Width="900">
  <Grid >
    <Rectangle Name="rec1" Width="100" Height="400" Fill="DimGray" RenderTransformOrigin="0,0">
      <Rectangle.RenderTransform>
        <RotateTransform x:Name="Rec1_Rotate"/>
      </Rectangle.RenderTransform>
    </Rectangle>

    <Button Width="45" Height="30" Name="Button1">Start
      <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard TargetProperty="Angle">                
                <DoubleAnimation Storyboard.TargetName="Rec1_Rotate" By="40" Duration="00:00:1" BeginTime="00:00:00"/>                                                
                <PointAnimation Storyboard.TargetName="rec1" Storyboard.TargetProperty="RenderTransformOrigin" To="0,1" Duration="00:00:0" BeginTime="00:00:02" />
                <DoubleAnimation Storyboard.TargetName="Rec1_Rotate" By="-80" Duration="00:00:2" BeginTime="00:00:03"/>
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>
      </Button.Triggers>
    </Button>
  </Grid>
</Window>

【问题讨论】:

  • 我不太明白您要做什么。你能不能稍微解释一下。据我了解,您试图将矩形从原始点向左旋转 40,然后您将点移动到 0,1,并且您希望它始终从那里旋转?您是否希望它从矩形的不同角旋转第二个动画?只是想对期望的结果进行一些澄清。
  • 我就是这样。我想从第一个动画的第一个点旋转。然后从不同的点再次移动,但保持先前的位置。我希望它更清楚

标签: wpf rotation rendertransform


【解决方案1】:

RenderTransformOrigin 指的是窗口中矩形的起点。当您基于点 (0,0) 旋转时,矩形将围绕自身的左上角旋转。如果您将 RenderTransformOrigin 更改为现在围绕 (0,1) 旋转,则您现在不会根据矩形的左下角在当前位置进行旋转,而是根据矩形的原始位置进行旋转。我对此的想法是将渲染变换原点设置为(0,0.5)之类的东西,以获得您正在寻找的摇摆运动。

【讨论】:

    【解决方案2】:

    这似乎是您正在寻找的,我使用了 Microsoft Expression Blend。您的锚点不在正确的位置(我不确定您是否正在为每个动画切换旋转点,但这假设您正在前后摇动它。

    <Window x:Class="SimpleMove"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OpenWWindow" Height="900" Width="900">
    <Window.Resources>
        <Storyboard x:Key="AnimateRectangle">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="rectangle">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="40"/>
                <EasingDoubleKeyFrame KeyTime="0:0:2" Value="-60"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource AnimateRectangle}"/>
            <BeginStoryboard Storyboard="{StaticResource AnimateRectangle}"/>
        </EventTrigger>
    </Window.Triggers>
    

    【讨论】:

    • 对不起,我不能让你的样品工作。您能否提供 Window 的完整代码。我实际上只使用 VS 2010
    猜你喜欢
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2011-05-20
    • 2013-05-22
    • 2018-11-25
    • 2014-03-04
    相关资源
    最近更新 更多