我在Windows 8 XAML中实现了一个模拟拨号,它几乎可以正常工作
然而,当转动拨盘时,它会像疯了一样震动。它在移动时似乎顺时针和逆时针旋转了近90度。我使用PointerPressed
,PointerReleased
和PointerMoved
我的代码
private void dial_PointerPressed(object sender, PointerRoutedEventArgs e)
{
dialPressed = true;
}
private void dial_PointerReleased(object sender, PointerRoutedEventArgs e)
{
dialPressed = false;
}
private void dial_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if(dialPressed)
{
Windows.UI.Input.PointerPoint currentLocation =
e.GetCurrentPoint(dial);
Point dialCenter =
new Point(dial.ActualHeight / 2, dial.ActualWidth / 2);
double radians = Math.Atan(
(currentLocation.Position.Y - dialCenter.Y) /
(currentLocation.Position.X - dialCenter.X));
RotateTransform r = new RotateTransform();
r.Angle = radians * 180 / Math.PI;
if (currentLocation.Position.X - dialCenter.X < 0)
{
r.Angle += 180;
}
TransformGroup t = new TransformGroup();
t.Children.Add(r);
dial.RenderTransform = t;
}
}
我应该对不同的事件有约束力吗?是什么让我的表盘摇摇欲坠?
答案 0 :(得分:0)
好的,我解决了我的问题。
代码来自WPF示例,Windows 8中有一些不同之处!这对我有用。
if (dialPressed)
{
// Get the current mouse position relative to the dial
// I was doing this relative to the dial that was turning, which is wrong.
// The position of the pointer relative to the dial will change as the dial turns!
// I created a static dial behind it, the same dimensions
Windows.UI.Input.PointerPoint currentLocation =
e.GetCurrentPoint(this.dial_Static);
Point dialCenter =
new Point(this.dial.ActualHeight / 2, this.dial.ActualWidth / 2);
// Calculate an angle
double radians = Math.Atan((currentLocation.Position.Y - dialCenter.Y) /
(currentLocation.Position.X - dialCenter.X));
// in order to get these figures to work, I actually had to *add* 90 degrees to it,
// and *subtract* 180 from it if the X coord is negative.
double x = (radians * 180 / Math.PI) + 90;
if ((currentLocation.Position.X - dialCenter.X) < 0)
{
x = x - 180;
}
RotateTransform r = new RotateTransform();
r.Angle = x;
TransformGroup t = new TransformGroup();
t.Children.Add(r);
dial.RenderTransform = t;
}
我知道这段代码可以编写得更整齐,但是我试图将它保持在接近上面的版本以显示更改的内容。