我在我正在建造的纸牌游戏应用程序中使用了三个画布。一个是主帆布,其他两个作为儿童画布(一个静态和一个将旋转):
在这个示例应用程序中,我想将RotatingEl移动到StaticEl上的位置:
当我单击“移动”按钮时,它按预期工作:
现在,我想旋转RotatingCanvas并仍然将RotatingEl移动到StaticEl位置并调整旋转以仍然匹配StaticEl的角度:
当我尝试时,它移动到错误的位置:
以下是移动按钮上的代码点击:
GeneralTransform generalTransformStaticEl = StaticEl.TransformToVisual(MainCanvas);
Point pointstatic = generalTransformStaticEl.Transform(new Point());
GeneralTransform generalTransformRotEl = RotatingEl.TransformToVisual(MainCanvas);
Point pointrot = generalTransformRotEl.Transform(new Point());
double distancecalcX = pointstatic.X - pointrot.X;
double distancecalcY = pointstatic.Y - pointrot.Y;
DoubleAnimation ELMoveY = new DoubleAnimation();
ELMoveY.From = Canvas.GetTop(RotatingEl);
ELMoveY.To = Canvas.GetTop(RotatingEl)+(distancecalcY);
ELMoveY.Duration = new Duration(TimeSpan.FromSeconds(1.0));
DoubleAnimation ELMoveX = new DoubleAnimation();
ELMoveX.From = Canvas.GetLeft(RotatingEl);
ELMoveX.To = Canvas.GetLeft(RotatingEl)+(distancecalcX);
ELMoveX.Duration = new Duration(TimeSpan.FromSeconds(1.0));
RotatingEl.BeginAnimation(Canvas.LeftProperty, ELMoveX);
RotatingEl.BeginAnimation(Canvas.TopProperty, ELMoveY);
如何调整动画的“To”以将Rotated canvas的RotatingEl移动到静态StaticEl的位置并调整RotatingEl的旋转以匹配StaticEl的方向?
答案 0 :(得分:1)
我找到了自己的解决方案。如果有人可能感兴趣,这里是更新的代码:
double foundangle = 0;
//verify an actual transform group is there before getting rotate angle
if (RotatingCanvas.RenderTransform.Value.ToString() != "Identity")
{
RotateTransform rt = (RotatingCanvas.RenderTransform as TransformGroup).Children[2] as RotateTransform;
foundangle = rt.Angle;
}
RotateTransform rottrans = new RotateTransform(foundangle*-1);
RotatingEl.RenderTransform = rottrans;
GeneralTransform generalTransformStaticEl = StaticEl.TransformToVisual(RotatingCanvas);
Point pointstatic = generalTransformStaticEl.Transform(new Point());
DoubleAnimation ELMoveY = new DoubleAnimation();
ELMoveY.From = Canvas.GetTop(RotatingEl);
ELMoveY.To = pointstatic.Y;
ELMoveY.Duration = new Duration(TimeSpan.FromSeconds(1.0));
DoubleAnimation ELMoveX = new DoubleAnimation();
ELMoveX.From = Canvas.GetLeft(RotatingEl);
ELMoveX.To = pointstatic.X;
ELMoveX.Duration = new Duration(TimeSpan.FromSeconds(1.0));
RotatingEl.BeginAnimation(Canvas.LeftProperty, ELMoveX);
RotatingEl.BeginAnimation(Canvas.TopProperty, ELMoveY);
generalTransformStaticEl需要从旋转的画布而不是非旋转的MainCanvas进行坐标。对旋转的调整只是获取旋转画布的当前旋转角度并乘以-1以与静态矩形对齐