本文介绍了WPF中的简单2D动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我将使用WPF编写图形化的N拼图.
我的问题是,如何在延迟的方向上移动图像.

让我举一个例子来澄清我的问题:
图像位于窗口中心.在2秒钟内向左平滑移动约80像素,然后在2秒钟内向下移动80像素,然后向右移动,然后向上...

我想知道如何用C#代码完成.

Hi!

I''m going to write a graphical N-puzzle using WPF.
my question is that,how could I move an image in directions that I want,with delay.

let me give you an example to clarify my question:
An image is located in center of window.and moves smoothly to left about 80 pixels in 2 seconds,then moves to down 80 pxls in 2 secs,then right,then up...

I want to know how it is done in C# code.

推荐答案

public static void MoveTo(this Image target, double newX, double newY)
    {
        var top = Canvas.GetTop(target);
        var left = Canvas.GetLeft(target);
        TranslateTransform trans = new TranslateTransform();
        target.RenderTransform = trans;
        DoubleAnimation anim1 = new DoubleAnimation(top, newY - top, TimeSpan.FromSeconds(10));
        DoubleAnimation anim2 = new DoubleAnimation(left, newX - left, TimeSpan.FromSeconds(10));
        trans.BeginAnimation(TranslateTransform.XProperty,anim1);
        trans.BeginAnimation(TranslateTransform.YProperty,anim2);
    }



这篇关于WPF中的简单2D动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:23