c# - 动画网格从一个位置到另一个

标签 c# wpf xaml animation grid

我有一个图像和按钮网格,我想自动设置从一个位置到另一个位置(实际上是向左几个空格)的动画,但它没有用。我试过在 xaml 中使用 Storyboard,并按照下面的代码以编程方式使用,但它现在可以工作了。请帮忙!!!

    public static void MoveTo(Grid target)
    {
        Canvas.SetLeft(target, 0);

        var top = Canvas.GetTop(target);
        var left = Canvas.GetLeft(target);
        TranslateTransform trans = new TranslateTransform();
        target.RenderTransform = trans;
        double newX = (double)(left - 300);
        double newY = (double)top;
        DoubleAnimation anim1 = new DoubleAnimation(top, -15, TimeSpan.FromSeconds(10));
        //DoubleAnimation anim1 = new DoubleAnimation(top, newY - top, TimeSpan.FromSeconds(10));

        DoubleAnimation anim2 = new DoubleAnimation(left, newX - left, TimeSpan.FromSeconds(10));
        anim1.AutoReverse = true;
        anim1.RepeatBehavior = RepeatBehavior.Forever;
        trans.BeginAnimation(TranslateTransform.XProperty, anim1);
        trans.BeginAnimation(TranslateTransform.YProperty, anim2);
    }

最佳答案

完全 TranslateTransform 不适合你想要的。最好使用 thinkness 动画

我建议您不要使用 canavas 并将其更改为网格,但如果您使用 canavas,请使用此代码

 private void Animationsss(Grid grd)
        {
            //create an animation
            DoubleAnimation da = new DoubleAnimation();
            //set from animation to start position 
            //dont forget set canvas.left for grid if u dont u will get error
            da.From = Canvas.GetLeft(grd);
            //set second position of grid
            da.To = -100;
            //set duration
            da.Duration = new Duration(TimeSpan.FromSeconds(2));
            //run animation if u want stop ,start etc use story board
            grd.BeginAnimation(Canvas.LeftProperty, da);

        }

如果你使用网格代码是这样的:

 private void Animation(Grid grd)
        {
            ThicknessAnimation ta = new ThicknessAnimation();
            //your first place
            ta.From = grd.Margin;
            //this move your grid 1000 over from left side
            //you can use -1000 to move to left side
            ta.To = new Thickness(1000, 0, 0, 0);
            //time the animation playes
            ta.Duration = new Duration(TimeSpan.FromSeconds(10));
            //dont need to use story board but if you want pause,stop etc use story board
            grd.BeginAnimation(Grid.MarginProperty, ta);
        }

您可以使用不透明度动画来淡化您的网格...如果移动和淡化它显示效果很好!

    private void Animationsss(Grid grd)
        {
            DoubleAnimation da = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(2)));
            grd.BeginAnimation(Grid.OpacityProperty, da);
        }

如果没有任何原因,您可以将 canavas 更改为网格,并且更好地使用 TranslateTransform 调整大小控件,例如如果鼠标进入此代码调整大小控件下面的代码:

private void grid1_MouseEnter(object sender, MouseEventArgs e)
        {
            //at first its normal size
            ScaleTransform st = new ScaleTransform(1, 1);
            //animation size to 1.25 persent of real size
            DoubleAnimation da = new  DoubleAnimation(1,1.25, new Duration(TimeSpan.FromSeconds(2)));
            //set transform to control
            grid1.RenderTransform = st;
            //animation transform now From Y And X
            st.BeginAnimation(ScaleTransform.ScaleXProperty, da);
            st.BeginAnimation(ScaleTransform.ScaleYProperty, da);
        }

如果你的宽度或高度动画做同样的工作,比如缩放变换:)

希望我能帮到你...:))

请给我留言

关于c# - 动画网格从一个位置到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10459289/

相关文章:

c# - 图形 DrawString() 剪切文本,而矩形仍有更多空间

c# - 我可以动态地将 TextBlock.Text 的一部分设置为不同的颜色吗?

c# - 更改属性时遇到问题

c# - 如何使用 iText7 将 .p7s 字节数组插入到 PDF 中?

c# - 过程或函数 ' ' 需要未提供的参数 ' '

c# - 需要获取当前网络服务器名称

c# - 在运行时更改 DataTrigger 绑定(bind)引用

wpf - MVVM : Binding Commands with Collection to Listbox in WPF

c# - 获取设置为自动的网格的实际宽度

c# - 您如何让 XAML 元素缩放以适合其容器?