简体   繁体   English

WPF TranslateTransform

[英]WPF TranslateTransform

Im trying to use the TranslateTransform class to move a image on a Grid on Y axis. 我试图使用TranslateTransform类在Y轴上的Grid上移动图像。 I need this movment to be smooth so i can not use SetMargin or SetCanvas. 我需要这个运动是平稳的,所以我不能使用SetMargin或SetCanvas。 I try this in code behind: 我在后面的代码中尝试此操作:

public void MoveTo(Image target, double oldY, double newY)
{
    var trans = new TranslateTransform();
    var anim2 = new DoubleAnimation(0, newY, TimeSpan.FromSeconds(2))
                    {EasingFunction = new SineEase()};
    target.RenderTransform = trans;
    trans.BeginAnimation(TranslateTransform.YProperty, anim2);
}

The object i want to use (a Image control) is placed on a Grid. 我要使用的对象(图像控件)放置在网格上。 For the first time everything works fine. 第一次一切正常。 The problems comes when i try to move the object again using the same function. 当我尝试使用相同的功能再次移动对象时,问题就来了。 The object (a Image control) first move to the start position (initial Y coordinate) then the animation begins. 对象(图像控件)首先移动到开始位置(初始Y坐标),然后动画开始。

Is it not suposed for the TranslateTransform to change the coordinates (in my case the Margin property) too? TranslateTransform是否也不必更改坐标(在我的情况下为Margin属性)?

Thank you. 谢谢。

The transform does not change the original values.they are your point of origin. 转换不会更改原始值。它们是您的起点。 If you want a new point of origin each time you move you can handle the animation completed event. 如果每次移动都需要一个新的原点,则可以处理动画完成事件。 Or from your transform you can get your current offset and make that your new start point for the animation. 或者,您可以从变换中获取当前的偏移量,并将其作为动画的新起点。

In other words your start values would always be your last move to values 换句话说,您的起始价值观永远是您走向价值观的最后一步

You've explicitly told the animation to start from 0. It's doing what you've told it. 您已经明确地告诉动画从0开始。它正在按照您所说的进行操作。 Just remove the explicit zero fromvalue and everything will work. 只需删除显式的零fromvalue ,一切都会起作用。

var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2)) 
                { EasingFunction = new SineEase() };

You have to use the By property of DoubleAnimation. 您必须使用DoubleAnimation的By属性。 Try that: 试试看:

//everytime you execute this anmation your object will be moved 2.0 further
double offset = 2.0 
var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2));
anim2.To = null;
anim2.By = offset;

The TranslateTransform is a specific kind of render transformation. TranslateTransform是一种特定的渲染转换。 Rather that changing properties of the control (such as the Margin property), it simply affects how the control is displayed on the screen. 而不是更改控件的属性(例如Margin属性),它只是影响控件在屏幕上的显示方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM