围绕手势点旋转/缩放

时间:2017-02-24 10:11:08

标签: uwp gesture

我希望通过手势移动/缩放/旋转用户控件,我希望旋转和缩放使其中心点位于手势的中心(例如,当使用两个手指旋转时,手指之间的点应该是旋转的中心)。

当我不尝试设置旋转/缩放的中心点或设置静态点时,一切都按预期工作。将CompositeTransform.CenterX / Y设置为ManipulationDeltaRoutedEventArgs.Position的值时,usercontrol将以每个手势更加错误的中心点旋转,并且偶尔会加速。

我正在使用CompositeTransform作为我的用户控件的渲染转换,我已经连接到ManipulationDelta事件,如下所示:

private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        //this.transform is my composite transform 
        //which is set to be the render transform of the user control
        this.transform.CenterX = e.Position.X;
        this.transform.CenterY = e.Position.Y;
        this.transform.ScaleX *= e.Delta.Scale;
        this.transform.ScaleY *= e.Delta.Scale;
        this.transform.Rotation += e.Delta.Rotation;
        this.transform.TranslateX += e.Delta.Translation.X;
        this.transform.TranslateY += e.Delta.Translation.Y;
    }

似乎e.Position没有给我我想要的东西,不幸的是文档非常简短,只是陈述Gets the point from which the manipulation originated.从我的调试版本看,它似乎是CompositeTransform.CenterX / Y和ManipulationDeltaRoutedEventArgs。位置在用户控件的坐标系中。

1 个答案:

答案 0 :(得分:1)

问题原来是CompositeTransform只能处理一个中心点。因此,当中心点改变时,它也会对所有先前的转换进行追溯性改变。解决方案是使用TransformGroup并使用自己的中心点创建单独的转换:

private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        var localCoords = e.Position;
        var relativeTransform = this.TransformToVisual(this.Container);
        Point parentContainerCoords = relativeTransform.TransformPoint(localCoords);
        var center = parentContainerCoords;

        RotateTransform rotation = new RotateTransform();
        rotation.CenterX = center.X;
        rotation.CenterY = center.Y;
        rotation.Angle = e.Delta.Rotation;
        this.transformGroup.Children.Add(rotation);

        ScaleTransform scaling = new ScaleTransform();
        scaling.CenterX = center.X;
        scaling.CenterY = center.Y;
        scaling.ScaleX = e.Delta.Scale;
        scaling.ScaleY = e.Delta.Scale;
        this.transformGroup.Children.Add(scaling);

        TranslateTransform translation = new TranslateTransform();
        translation.X = e.Delta.Translation.X;
        translation.Y = e.Delta.Translation.Y;
        this.transformGroup.Children.Add(translation);
    }
相关问题