动画Canvas.Left属性 [英] Animate Canvas.Left property

查看:466
本文介绍了动画Canvas.Left属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的动画图像左属性,在画布。

I'm trying to animate the left property of my image that is in a canvas.

当我这样做:

image.SetValue(Canvas.LeftProperty, destX[i]);

这是工作。
但是,当我做:

this is working. But when I'm doing :

Animate(image, lastValue, destX[i], 500);

    private void Animate(Image image, double val1, double val2, double miliseconds)
    {
        DoubleAnimation myDoubleAnimation = new DoubleAnimation { From = val1, To = val2, Duration = new Duration(TimeSpan.FromMilliseconds(miliseconds)) };

        TranslateTransform ts = new TranslateTransform();
        image.RenderTransform = ts;
        Storyboard.SetTarget(myDoubleAnimation, ts);

        Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(TranslateTransform.XProperty));
        Storyboard myMovementStoryboard = new Storyboard();
        myMovementStoryboard.Children.Add(myDoubleAnimation);
        myMovementStoryboard.Begin();
        myMovementStoryboard.Completed += (s, e) =>
            {
                image.SetValue(Canvas.LeftProperty, val2);
            };
    }

它不工作

有什么可以错在我的动画功能?
即使动画可不好做,完成事件应在良好的价值重置为canvas.leftproperty。

What can be wrong in my Animate function ? Even if animation can be bad done, the completed event should reset the good value to canvas.leftproperty.

但在我的情况下,某样东西是错误的。

But in my case, something gets wrong.

你会如何做了动画功能?

How would you have done the Animate function ?

在此先感谢您的帮助。

推荐答案

您正在设定Canvas.LeftProperty,但你动画TranslateTransform.XProperty。这些都是非常不同的性质,因为翻译变换的布局后传中应用的。因此,对象是在画布使用Canvas.Left布局,但随后的TranslateTransform导致它要绘制从最初的产地转移。你应该做的是设置TranslateTransform.XProperty回val2的。

You're setting Canvas.LeftProperty, but you're animating TranslateTransform.XProperty. These are very different properties, as the translate transform is applied after the layout pass. So the object is laid out in the Canvas using Canvas.Left, but then the TranslateTransform causes it to be drawn shifted from that original origin. What you should do is set the TranslateTransform.XProperty back to val2.

myMovementStoryboard.Completed += (s, e) =>
            {
                ts.X = val2;
            };

这是说,动画应该不是真的失败在中间,所以这个额外的保护不应该是必要的。

That said, the animation should not really fail in the middle, so this extra safeguard should not be necessary.

这篇关于动画Canvas.Left属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆