我正在尝试对 UI 元素(矩形)进行操作,并且可以毫无问题地旋转和翻译它。我想要实现的是使另一个 UI 元素(例如椭圆)跟随第一个(矩形)。
如果我将用于矩形的相同变换组应用于椭圆,则在平移操作期间它可以正常工作,但在旋转期间椭圆不跟随矩形。
我想我必须以某种方式找到一个合适的复合变换中心点来提供给椭圆,但我不知道怎么做。
这是相应的示例代码。
public MainPage()
{
this.InitializeComponent();
rectMy.ManipulationMode = ManipulationModes.None | ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.Rotate;
rectMy.ManipulationStarted += rectMy_ManipulationStarted;
rectMy.ManipulationDelta += rectMy_ManipulationDelta;
rectMy.ManipulationCompleted += rectMy_ManipulationCompleted;
transformGroup.Children.Add(previousTransform);
transformGroup.Children.Add(compositeTransform);
rectMy.RenderTransform = transformGroup;
}
void rectMy_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
e.Handled = true;
}
void rectMy_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
previousTransform.Matrix = transformGroup.Value;
Point center = previousTransform.TransformPoint(new Point(rectMy.Width / 2, rectMy.Height / 2));
compositeTransform.CenterX = center.X;
compositeTransform.CenterY = center.Y;
compositeTransform.Rotation = e.Delta.Rotation;
compositeTransform.ScaleX = compositeTransform.ScaleY = e.Delta.Scale;
compositeTransform.TranslateX = e.Delta.Translation.X;
compositeTransform.TranslateY = e.Delta.Translation.Y;
}
void rectMy_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
e.Handled = true;
}