2011-11-15 17 views
1

私は3つのキャンバスを使用しています。一つは子供のキャンバス(1静的および回転します1)として、他の二つを持っている主なキャンバスです:このサンプルアプリでWPF TranslateTransformからキャンバス内のオブジェクトを別のキャンバス内の固定オブジェクトに変換

Canvas Layout

、私はStaticEl上の位置にRotatingElを移動します:

Start Point

私は移動するボタンをクリックすると、予想通り、それは動作します:

End Point

私はそれをしようとすると

Rotated Canvas

が、それは間違っているに移動:0

は今、私はRotatingCanvasを回転させ、まだStaticElの場所にRotatingElの動きを持って、まだStaticElの角度に一致するように回転を調整したいです場所:

End Location After Move

ここでは、移動ボタンをクリックして上の私のコードです:

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」を調整しても、回転キャンバスのRotatingElを静的StaticElの位置に移動し、RotatingElの回転をStaticElの向きに合わせて調整できますか?

答えて

1

私自身の解決策が見つかりました。 generalTransformStaticEl代わりに非回転MainCanvasの回転キャンバスから座標を必要

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

:場合には誰でも興味がある可能性があり、ここで更新されたコードです。回転の調整は、回転キャンバスの現在の回転角度を取得し、固定矩形に合わせて-1を掛けることの問題でした。

関連する問題