【问题标题】:Double Animation - What is TranslateTransform "Width Change" Definition in C# UWP?双动画 - 什么是 C# UWP 中的 TranslateTransform“宽度变化”定义?
【发布时间】:2018-08-24 07:24:03
【问题描述】:

我正在使用 MSDN 示例在 C3 UWP 中设置 Rectangle 的宽度动画。 下面是示例代码:

private void Create_And_Run_Animation()
    {

        // Create a red rectangle that will be the target
        // of the animation.
        Rectangle myRectangle = new Rectangle();
        myRectangle.Width = 0;
        myRectangle.Height = 20;
        SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
        myRectangle.Fill = myBrush;

        // Create the transform
        TranslateTransform stretchTransform = new TranslateTransform();
        stretchTransform.X = 0;
        stretchTransform.Y = 0;
        myRectangle.RenderTransform = stretchTransform;

        // Add the rectangle to the tree.
        DefaultLayout.Children.Add(myRectangle);
        myRectangle.Name = "myWidthAnimatedRectangle";
        // Create a duration of 2 seconds.
        Duration duration = new Duration(TimeSpan.FromSeconds(2));
        // Create two DoubleAnimations and set their properties.
        DoubleAnimation myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.From = 0.0;

        myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
        Storyboard justintimeStoryboard = new Storyboard();
        justintimeStoryboard.Duration = duration;
        justintimeStoryboard.Children.Add(myDoubleAnimation);

        Storyboard.SetTarget(myDoubleAnimation, stretchTransform);

        Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);

        // Set the X  property of the Transform to be the target property

        Storyboard.SetTargetProperty(myDoubleAnimation, "X");
        myDoubleAnimation.To = 300.0;


        // Make the Storyboard a resource.
        DefaultLayout.Resources.Add("justintimeStoryboard", justintimeStoryboard);
        // Begin the animation.
        justintimeStoryboard.Begin();
    }

但是,在 Build 中,它所做的是移动对象而不是更改其宽度。 我需要动画宽度变化。

【问题讨论】:

    标签: c# animation uwp storyboard doubleanimation


    【解决方案1】:

    但是,在 Build 中,它所做的是移动对象而不是更改其宽度。我需要动画宽度变化。

    您的代码用于为 Rectangle 的位置而不是其宽度设置动画。您可以为其宽度指定默认值并为其ScaleTransform 设置动画。

    如果必须使用DoubleAnimation 为其宽度设置动画,则需要将EnableDependentAnimation 设置为true。请检查以下代码示例:

    private void Create_And_Run_Animation()
    {
        // Create a red rectangle that will be the target
        // of the animation.
        Rectangle myRectangle = new Rectangle();
        myRectangle.Width = 10;
        myRectangle.Height = 20;
        SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
        myRectangle.Fill = myBrush;
    
        // Add the rectangle to the tree.
        DefaultLayout.Children.Add(myRectangle);
        myRectangle.Name = "myWidthAnimatedRectangle";
    
        DoubleAnimation myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.From = 0.0;
        myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
    
        Storyboard justintimeStoryboard = new Storyboard();
        Storyboard.SetTargetProperty(myDoubleAnimation, "(Rectangle.Width)");
        Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
    
        myDoubleAnimation.To = 300.0;
        myDoubleAnimation.EnableDependentAnimation = true;
        justintimeStoryboard.Children.Add(myDoubleAnimation);
    
        // Make the Storyboard a resource.
        DefaultLayout.Resources.Add("justintimeStoryboard", justintimeStoryboard);
        // Begin the animation.
        justintimeStoryboard.Begin();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 2012-05-17
      • 2021-02-21
      • 2020-01-13
      相关资源
      最近更新 更多