【问题标题】:How to rotate uwp usercontrol smoothly?如何平滑地旋转 uwp 用户控件?
【发布时间】:2019-03-12 04:16:55
【问题描述】:

我创建了一个包含一些文本框、图像等的用户控件。现在我想用鼠标旋转该用户控件。我已经成功旋转,但是在旋转过程中它倒转,我在哪里做错了?下面是我的 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;
    }
}

【问题讨论】:

  • 在 Blend 中执行此操作不是更容易/更清洁/更顺畅吗?
  • 我没有使用情节提要来旋转这个元素,正如你所说的那样更容易。但是,我希望使用鼠标为用户提供旋转功能,以这种方式实现。用户必须使用鼠标旋转元素。

标签: c# uwp


【解决方案1】:

您的问题令人困惑...您想在屏幕平面上旋转 2D 对象,对吗? (这就是我从你的转换中假设的。)那么你为什么同时使用 x 和 y 鼠标位置?您的旋转是一个标量值,因此您应该只使用鼠标移动的 1 轴。

如果您想通过让鼠标抓住对象周围的假想环并旋转它来旋转对象...那么您应该保持对相对于对象中心或起始角度的起始位置的引用鼠标相对于对象的中心。可能是这样的:

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;
    }
}

【讨论】:

  • 我想在屏幕平面上围绕它的中心旋转我的 2D 对象。不在虚环的路径上。我的代码旋转但意外反转到 360 度,无法解决这个问题!如果没有 x 和 y 位置,我怎么能做到这一点?如果我只使用X位置,我怎么知道它是顺时针旋转还是逆时针旋转?
  • @MgBhadurudeen 请检查您是否使用过RotateInertia 模型。更多你可以参考uwp基本输入代码sample
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 2013-12-05
  • 1970-01-01
相关资源
最近更新 更多