使用ManipulationDelta Event UWP

时间:2018-08-08 15:46:26

标签: c# uwp manipulationdelta

我想用惯性旋转边界。我在哪里丢东西?

MainPage.xaml:

<Grid>
    <Border x:Name="ManipulationBorder" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Red"/>
</Grid>

MainPage.xaml.cs:

private TransformGroup transforms;
private MatrixTransform previousTransform;
private CompositeTransform deltaTransform;

public MainPage()
{
    this.InitializeComponent();

    InitManipulationTransforms();

    ManipulationBorder.ManipulationDelta += new ManipulationDeltaEventHandler(ManipulateMe_ManipulationDelta);

    ManipulationBorder.ManipulationMode =
        ManipulationModes.TranslateX |
        ManipulationModes.TranslateY |
        ManipulationModes.Rotate |
        ManipulationModes.TranslateInertia |
        ManipulationModes.RotateInertia;
}

private void InitManipulationTransforms()
{
    transforms = new TransformGroup();
    previousTransform = new MatrixTransform() { Matrix = Matrix.Identity };
    deltaTransform = new CompositeTransform();

    transforms.Children.Add(previousTransform);
    transforms.Children.Add(deltaTransform);

    ManipulationBorder.RenderTransform = transforms;
}

private void ManipulateMe_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    previousTransform.Matrix = transforms.Value;

    // Center point for rotation
    Point center = previousTransform.TransformPoint(new Point(e.Position.X, e.Position.Y));
    deltaTransform.CenterX = center.X;
    deltaTransform.CenterY = center.Y;

    // Rotation
    deltaTransform.Rotation = e.Delta.Rotation;
}

当我实际上去在ManipulationDeltaRoutedEventArgs中应用增量旋转时,它不起作用。我在哪里错了?

谢谢。

1 个答案:

答案 0 :(得分:1)

  

当我实际上去在ManipulationDeltaRoutedEventArgs中应用增量旋转时,它不起作用。我在哪里错了?

基于触摸的标准旋转需要两个或更多个触摸点:

enter image description here

对于单点触摸旋转,您需要首先固定deltaTransform的中心。源自DIEDERIK-KROLS博客Creating touch-based rotation in Windows 8 XAML Store apps。 您需要重新计算一次Angle的更改。

enter image description here

从上面的屏幕截图中,您将知道Angle = a1 - a2的值。

private void Right_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{

    var x = this.RightRotateTransform.CenterX - e.Position.X;
    var y = this.RightRotateTransform.CenterY - e.Position.Y;

    double a1 = Math.Atan(y / x);
    double a2 = Math.Atan((e.Delta.Translation.Y - y) / (x - e.Delta.Translation.X));

    this.RightRotateTransform.Angle += a1 - a2;
}

博客样本已过期,我用UWP最新版本重写了它。这是sample链接。