0

我正在尝试对 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;
    }
4

1 回答 1

0

好的。我现在更好地理解它并找到了解决方案。这完全是关于复合变换的中心点(正如我最初猜测的那样)。对于椭圆的中心,我必须提供矩形的中心。然而,需要给出相对于椭圆的坐标。在我的例子中,椭圆位于矩形的右上角,所以下面是我给出的复合变换中心。

Point centerE = previousTransformE.TransformPoint(new Point(-rectMy.Width / 2 + ellipseMy.Width / 2, rectMy.Height / 2 + ellipseMy.Height / 2));

对于矩形,复合变换的中心点是:

Point center = previousTransform.TransformPoint(new Point(rectMy.Width / 2, rectMy.Height / 2));

Stackoverflow 不允许我发布图像以更好地可视化事物。对不起!

整个代码:

previousTransform.Matrix = transformGroup.Value;
previousTransformE.Matrix = transformGroupE.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.TranslateX = e.Delta.Translation.X;
compositeTransform.TranslateY = e.Delta.Translation.Y;

Point centerE = previousTransformE.TransformPoint(new Point(-rectMy.Width / 2 + ellipseMy.Width / 2, rectMy.Height / 2 + ellipseMy.Height / 2));
compositeTransformE.CenterX = centerE.X;
compositeTransformE.CenterY = centerE.Y;

compositeTransformE.Rotation = e.Delta.Rotation;
compositeTransformE.TranslateX = e.Delta.Translation.X;
compositeTransformE.TranslateY = e.Delta.Translation.Y;
于 2012-10-14T21:04:02.803 回答