0

我在 Windows 8 XAML 中实现了一个模拟拨号,它几乎可以工作

但是,当转动表盘时,它会像疯了一样摇晃。它似乎在移动时顺时针和逆时针旋转了近 90 度。我已经使用PointerPressed,PointerReleasedPointerMoved

我的代码

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;

    }
}

我应该对不同的事件有约束力吗?是什么让我的表盘抖动?

4

1 回答 1

1

好的,我解决了我的问题。

该代码来自 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;

}

我知道这段代码可以写得更整洁,但我试图让它尽可能接近上面的版本,以显示发生了什么变化。

于 2012-11-07T11:44:09.403 回答