简体   繁体   English

如何顺利旋转uwp用户控件?

[英]How to rotate uwp usercontrol smoothly?

I have created a user control having some textboxes, images, etc. Now I want to rotate that usercontrol with mouse. 我创建了一个包含一些文本框,图像等的用户控件。现在,我想用鼠标旋转该用户控件。 I have managed to rotate successfully, but it inverts during rotation, where am I doing mistake? 我已经成功地旋转了,但是在旋转过程中它会反转,我在哪里做错了? Below is my xaml & code behind: 下面是我的XAML和代码背后:

<UserControl...
    <Grid x:Name="ContainerGrid"
    Width="300" Height="400" RenderTransformOrigin="0.5,0.5"
    ManipulationMode="TranslateX,TranslateY,Rotate"
    ManipulationStarted="Manipulator_OnManipulationStarted"
    ManipulationDelta="Manipulator_OnManipulationDelta">

    <Grid.RenderTransform>
    <RotateTransform x:Name="RotateGrid" Angle="0"/>
    </Grid.RenderTransform>

    <!-- some elements -->
    <Rectangle x:Name="RotateRectangle"
    IsHitTestVisible="False"
    Width="16" Height="16"
    Fill="Red"
    VerticalAlignment="Top"
    HorizontalAlignment="Right" />
</Grid>


private void Manipulator_OnManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
    if (e.Position.X > ContainerGrid.Width - RotateRectangle.Width && 
    e.Position.Y < ContainerGrid.Height - RotateRectangle.Height)
    {
    _isRotating = true;
    lastPosition = e.Position;
    return;
    }

    _isRotating = false;
}


private void Manipulator_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{ 
    if (_isRotating)
    {
    Point currentLocation = e.Position;
    double radians = Math.Atan2((currentLocation.Y - lastPosition.Y),     (currentLocation.X - lastPosition.X));
    var angle = radians * 180 / Math.PI;
    RotateGrid.Angle = angle;
    lastPosition = currentLocation;
    }
}

Your question is confusing... you want to rotate the 2D object in the plane of the screen, right? 您的问题令人困惑...您想在屏幕平面上旋转2D对象,对不对? (That's what I assumed from your transformation.) So why are you using both x and y mouse positions? (这就是我从您的变换中得出的假设。)那么,为什么同时使用x和y鼠标位置? Your rotation is a scalar value, so you should just be using 1-axis of your mouse movement. 旋转是一个标量值,因此您应该只使用鼠标移动的1轴。

If you instead want to rotate your object by having your mouse grab an imaginary ring around the object and rotate that... then you should instead be keeping a reference to your start position relative to the object's center or the starting angle of the mouse relative to the object's center. 相反,如果您想让鼠标抓住假想的环来旋转对象并旋转该对象,则应该...相对于对象中心的起始位置或鼠标的起始角度作为参考到对象的中心。 Something like this maybe: 可能是这样的:

private void Manipulator_OnManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
    if (e.Position.X > ContainerGrid.Width - RotateRectangle.Width && 
    e.Position.Y < ContainerGrid.Height - RotateRectangle.Height)
    {
    _isRotating = true;
    var startingRadians = Math.Atan2((currentLocation.Y - objectCenter.Y),     (currentLocation.X - objectCenter.X));
    startingAngle = startingRadians * 180 / Math.PI;
    return;
    }

    _isRotating = false;
}

private void Manipulator_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{ 
    if (_isRotating)
    {
    Point currentLocation = e.Position;
    double radians = Math.Atan2((currentLocation.Y - objectCenter.Y),     (currentLocation.X - objectCenter.X));
    var angle = radians * 180 / Math.PI;
    RotateGrid.Angle = angle-startingAngle;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM