WPF 翻译转换

发布于 2024-11-27 09:08:24 字数 631 浏览 0 评论 0原文

我尝试使用 TranslateTransform 类在 Y 轴上的网格上移动图像。我需要这个运动平滑,所以我不能使用 SetMargin 或 SetCanvas。我在后面的代码中尝试这样做:

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);
}

我想要使用的对象(图像控件)放置在网格上。 第一次一切正常。 当我尝试使用相同的函数再次移动对象时,问题就出现了。 对象(图像控件)首先移动到起始位置(初始 Y 坐标),然后动画开始。

难道 TranslateTransform 不应该也改变坐标(在我的例子中是 Margin 属性)吗?

谢谢。

Im trying to use the TranslateTransform class to move a image on a Grid on Y axis. I need this movment to be smooth so i can not use SetMargin or 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.

Is it not suposed for the TranslateTransform to change the coordinates (in my case the Margin property) too?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坠似风落 2024-12-04 09:08:24

变换不会改变原始值。它们是您的原点。如果您每次移动时都想要一个新的原点,您可以处理动画完成事件。或者从您的变换中,您可以获得当前的偏移量并将其作为动画的新起点。

换句话说,你的起始价值观永远是你走向价值观的最后一步

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

薆情海 2024-12-04 09:08:24

TranslateTransform 是一种特定类型的渲染转换。它不会更改控件的属性(例如 Margin 属性),而只会影响控件在屏幕上的显示方式。

The TranslateTransform is a specific kind of render transformation. Rather that changing properties of the control (such as the Margin property), it simply affects how the control is displayed on the screen.

表情可笑 2024-12-04 09:08:24

您必须使用 DoubleAnimation 的 By 属性。
尝试一下:

//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;

You have to use the By property of DoubleAnimation.
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;
情栀口红 2024-12-04 09:08:24

您已经明确告诉动画从 0 开始。它正在执行您告诉它的操作。
只需删除显式的零fromvalue,一切都会正常。

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

You've explicitly told the animation to start from 0. It's doing what you've told it.
Just remove the explicit zero fromvalue and everything will work.

var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2)) 
                { EasingFunction = new SineEase() };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文