Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-旋转后平移时的触摸操作问题_C#_Wpf_Manipulationdelta - Fatal编程技术网

C# WPF-旋转后平移时的触摸操作问题

C# WPF-旋转后平移时的触摸操作问题,c#,wpf,manipulationdelta,C#,Wpf,Manipulationdelta,我对通过操纵事件(更准确地说是矩阵)进行对象转换有问题 问题:在第一次成功旋转/缩放/平移我的对象(顺便说一句:矩形)后,第二次操作(本例中为平移)将沿其新角度的方向移动对象。 例如,如果在第一次旋转(新角度为45°)后,我从右向左触摸屏幕,则对象将遵循45°的对角线路径,而不是我绘制的路径 期望值:我希望我的对象完全按照我在屏幕上所做的路径移动,而不管其旋转如何 情境:我通过代码在画布中放置一个矩形,这个矩形有“IsManipulationEnabled=true”和“renderTransf

我对通过操纵事件(更准确地说是矩阵)进行对象转换有问题

问题:在第一次成功旋转/缩放/平移我的对象(顺便说一句:矩形)后,第二次操作(本例中为平移)将沿其新角度的方向移动对象。 例如,如果在第一次旋转(新角度为45°)后,我从右向左触摸屏幕,则对象将遵循45°的对角线路径,而不是我绘制的路径

期望值:我希望我的对象完全按照我在屏幕上所做的路径移动,而不管其旋转如何

情境:我通过代码在画布中放置一个矩形,这个矩形有“IsManipulationEnabled=true”和“renderTransferorMorigin=new Point(.5.5)”和“mySuperRectangle.ManipulationDelta+=Container\u ManipulationDelta;”

这是我使用的代码:

    private void Container_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        try
        {
            if (e.OriginalSource != null)
            {
                Rectangle image = e.OriginalSource as Rectangle;


                Point center = new Point(image.ActualWidth / 2.0, image.ActualHeight / 2.0);
                Matrix imageMatrix = ((MatrixTransform)image.RenderTransform).Matrix;
                center = imageMatrix.Transform(center);

                // Move the Rectangle.
                imageMatrix.Translate(e.DeltaManipulation.Translation.X,
                                            e.DeltaManipulation.Translation.Y);

                // Rotate the Rectangle.
                imageMatrix.RotateAt(e.DeltaManipulation.Rotation,
                                     center.X,
                                     center.Y);

                // Resize the Rectangle. Keep it square 
                // so use only the X value of Scale.
                imageMatrix.ScaleAt(e.DeltaManipulation.Scale.X,
                                    e.DeltaManipulation.Scale.X,
                                    center.X,
                                    center.Y);

                // Apply changes
                image.RenderTransform = new MatrixTransform(imageMatrix);

                Rect containingRect =
                    new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);

                Rect shapeBounds =
                    image.RenderTransform.TransformBounds(
                        new Rect(image.RenderSize));

                // Check if the rectangle is completely in the window.
                // If it is not and intertia is occuring, stop the manipulation.
                if (e.IsInertial && !containingRect.Contains(shapeBounds))
                {
                    e.Complete();
                }

                e.Handled = true;
            }
        }
        catch (Exception)
        {
           throw;
        }
    }
有什么想法吗


Thanx很多。

您应该在画布上处理操纵事件,因为操纵的原点应该相对于固定的画布,而不是移动的矩形

<Canvas IsManipulationEnabled="True"
        ManipulationDelta="CanvasManipulationDelta">
    <Rectangle x:Name="rectangle" Width="200" Height="200" Fill="Red">
        <Rectangle.RenderTransform>
            <MatrixTransform/>
        </Rectangle.RenderTransform>
    </Rectangle>
</Canvas>
我的最终代码:

private void CanvasManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    var transform = rectangle.RenderTransform as MatrixTransform;
    var matrix = transform.Matrix;
    Point center = new Point(rectangle.ActualWidth / 2.0, rectangle.ActualHeight / 2.0);
    center = matrix.Transform(center);

    matrix.ScaleAt(e.DeltaManipulation.Scale.X , e.DeltaManipulation.Scale.X, center.X, center.Y);
    matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
    matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);

    rectangle.RenderTransform = new MatrixTransform(matrix);
}

编辑:在未检查
null
结果的情况下,不要将
用作
运算符。在这种情况下,如果
transform
为空,则创建一个新的MatrixTransform并将其分配给
RenderTransform
属性。然后,重用转换并只更新其
矩阵
属性

改进代码:

private void CanvasManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    var transform = rectangle.RenderTransform as MatrixTransform;

    if (transform == null) // here 
    {
        transform = new MatrixTransform();
        rectangle.RenderTransform = transform;
    }

    var matrix = transform.Matrix;
    var center = new Point(rectangle.ActualWidth / 2.0, rectangle.ActualHeight / 2.0);
    center = matrix.Transform(center);

    matrix.ScaleAt(e.DeltaManipulation.Scale.X , e.DeltaManipulation.Scale.X, center.X, center.Y);
    matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
    matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);

    transform.Matrix = matrix; // here
}

谢谢你@Clemens!你帮了我很大的忙!
private void CanvasManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    var transform = rectangle.RenderTransform as MatrixTransform;

    if (transform == null) // here 
    {
        transform = new MatrixTransform();
        rectangle.RenderTransform = transform;
    }

    var matrix = transform.Matrix;
    var center = new Point(rectangle.ActualWidth / 2.0, rectangle.ActualHeight / 2.0);
    center = matrix.Transform(center);

    matrix.ScaleAt(e.DeltaManipulation.Scale.X , e.DeltaManipulation.Scale.X, center.X, center.Y);
    matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
    matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);

    transform.Matrix = matrix; // here
}