【问题标题】:Run more timelines at the same同时运行更多时间线
【发布时间】:2014-03-20 03:59:00
【问题描述】:

我正在尝试通过代码而不是 XAML 在窗口中的 WPF 中学习一些东西。我希望有更多的时间线来同时变换对象(矩形)。我的意思是 RotateTransform 和 TransitionTransform 由一个命令调用。

    Rectangle aRectangle = new Rectangle();
    aRectangle.Width = 30;
    aRectangle.Height = 30;
    aRectangle.Fill = Brushes.Blue;

    TransformGroup g = new TransformGroup();

    RotateTransform rotateTransform = new RotateTransform();
    TranslateTransform animatedTranslateTransform = new TranslateTransform();


    g.Children.Add(rotateTransform);
    g.Children.Add(animatedTranslateTransform);

    aRectangle.RenderTransformOrigin = new Point(0.5, 0.5);
    aRectangle.RenderTransform = g;

    PathGeometry animationPath = new PathGeometry();
    PathFigure pFigure = new PathFigure();
    pFigure.StartPoint = new Point(0, 0);
    seg.Points.Add(new Point(0, 100));
    seg.Points.Add(new Point(100, 100));
    seg.Points.Add(new Point(100, 50));
    seg.Points.Add(new Point(50, 50));
    pFigure.Segments.Add(seg);
    animationPath.Figures.Add(pFigure);
    animationPath.Freeze();

    DoubleAnimationUsingPath translateXAnimation =
        new DoubleAnimationUsingPath();
    translateXAnimation.PathGeometry = animationPath;
    translateXAnimation.Duration = TimeSpan.FromSeconds(5);
    translateXAnimation.Source = PathAnimationSource.X;


    DoubleAnimationUsingPath translateYAnimation =
        new DoubleAnimationUsingPath();
    translateYAnimation.PathGeometry = animationPath;
    translateYAnimation.Duration = TimeSpan.FromSeconds(5);
    translateYAnimation.Source = PathAnimationSource.Y;
    DoubleAnimation translateAngle = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(5)));

如果我使用此代码一切正常:

    rotateTransform.BeginAnimation(RotateTransform.AngleProperty, translateAngle);
    animatedTranslateTransform.BeginAnimation(TranslateTransform.XProperty, translateXAnimation);
    animatedTranslateTransform.BeginAnimation(TranslateTransform.YProperty, translateYAnimation);

但这似乎很复杂,我试图了解如何将所有动画一起运行。我已经尝试过 Storyboard,但我必须使用 Canvas.Left 和 Top 来移动它,而且旋转也不起作用。

【问题讨论】:

  • 强烈建议不要在 WPF 的过程代码中创建或操作 UI 元素。你真的应该学习 XAML。
  • @HighCore - 总的来说我同意,但是当您需要根据数据大量自定义动画时,编写 C# 代码而不是在 XAML 中进行大量绑定可能更容易。
  • @ErnodeWeerd 现在不是 OP 的情况。在 WPF 中避免 XAML 就像在 C# 中避免 LINQ。
  • @HighCore - 虽然题外话:当我需要速度时我会避免使用 Linq,但通常我会使用它。他的问题很明确:他想知道如何在 XAML 中做到这一点。
  • ParallelTimeline 是您想要的吗?

标签: c# wpf


【解决方案1】:

并行运行动画的最佳方法是将它们全部添加到情节提要并启动情节提要。这可以在 C# 和 XAML 中完成。

XAML from this page 中的示例:

<Storyboard x:Key="sb" RepeatBehavior="Forever">
    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Width" From="1" To="350" 
                     Duration="0:0:1" BeginTime="0:0:0"/>

    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Height" From="1" To="250" 
                     Duration="0:0:1" BeginTime="0:0:1"/>

    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Height" From="250" To="1" 
                     Duration="0:0:1" BeginTime="0:0:2"/>

    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Width" From="350" To="1" 
                     Duration="0:0:1" BeginTime="0:0:3"/>
</Storyboard>

C# from this page中的示例:

DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 100;
myDoubleAnimation.To = 200;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));

ColorAnimation myColorAnimation = new ColorAnimation();
myColorAnimation.From = Colors.Blue;
myColorAnimation.To = Colors.Red;
myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush");
Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); 
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);
myStoryboard.Children.Add(myColorAnimation);

myStoryboard.Begin(this);

【讨论】:

  • 我不需要知道如何在 XAML 中进行操作。我需要知道如何在代码中做到这一点。我需要知道是否有机会将这些 BeginAnimation 方法放到一个简单的方法中。我从来没有在 C# 中这样做过,但是在 JavaFX 中,当我想一起运行更多转换时,我简单地将它们放入 ParallelTransition 并调用 Start 方法,仅此而已。我需要在路径上移动对象并在它移动时旋转它。
  • @joziff - 正如我所说:将动画放在一个故事板和故事板中。这将启动两个或所有动画。故事板是一种平行的时间线。
  • @joziff 我的答案中的 xaml 和代码示例没有链接或相互依赖。 xaml 中的任何内容都可以在代码中完成。如果你愿意,你可以忽略 xaml
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
相关资源
最近更新 更多