我创建了一个包含一些文本框,图像等的用户控件。现在,我想用鼠标旋转该用户控件。我已经成功地旋转了,但是在旋转过程中它会反转,我在哪里做错了?以下是我的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;
}
}
答案 0 :(得分:0)
您的问题令人困惑...您想在屏幕平面上旋转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;
}
}