2011-04-19 50 views
0

我發現這個很好的例子,它完美的作品。 我對數學非常不滿,所以旋轉和增加慣性非常容易。 我的問題是,我需要正常化旋轉角度爲0到360. 所以在完成一個方向旋轉到其他我可以從0到360,順時針或360到0(逆時針)選擇值 我會感激如果有人能幫助我。wpf操作旋轉

<UserControl x:Class="Rotatable.MyRotatableControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      IsManipulationEnabled="True" 
      ManipulationStarting="OnManipulationStarting" 
      ManipulationDelta="OnManipulationDelta" 
      ManipulationInertiaStarting="OnManipulationInertiaStarting" 
      RenderTransformOrigin="0.5,0.5"> 
    <UserControl.RenderTransform> 
     <RotateTransform x:Name="MyRotateTransform"/> 
    </UserControl.RenderTransform> 
    <Grid Background="Aqua"> 
     <Viewbox> 
      <TextBlock Text="Rotate me plz!"/> 
     </Viewbox> 
    </Grid> 
</UserControl> 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace Rotatable 
{ 
    /// <summary> 
    /// Interaction logic for MyRotatableControl.xaml 
    /// </summary> 
    public partial class MyRotatableControl : UserControl 
    { 
     public MyRotatableControl() 
     { 
      InitializeComponent(); 
     } 

     private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e) 
     { 
      e.Handled = true; 

      // Rotate the control according to the manipulation delta 
      MyRotateTransform.Angle += e.DeltaManipulation.Rotation; 
     } 

     private void OnManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e) 
     { 
      // Enable inertia by setting the initial rotation velocity and the desired ratio of deceleration 
      e.RotationBehavior = new InertiaRotationBehavior 
      { 
       InitialVelocity = e.InitialVelocities.AngularVelocity, 
       DesiredDeceleration = 0.001 
      }; 
     } 

     private void OnManipulationStarting(object sender, ManipulationStartingEventArgs e) 
     { 
      e.Handled = true; 

      UIElement container = VisualParent as UIElement; 

      // Enable single input rotation 
      e.IsSingleTouchEnabled = true; 
      e.ManipulationContainer = container; 

      // Only allow rotation 
      e.Mode = ManipulationModes.Rotate; 

      // Set the pivot 
      Point localCenter = new Point(ActualWidth/2, ActualHeight/2); 
      Point localCenterInContainer = TranslatePoint(localCenter, container); 
      e.Pivot = new ManipulationPivot(localCenterInContainer, Math.Max(ActualWidth/2, ActualHeight/2)); 
     } 
    } 
} 

回答

1

任何變換WPF - 這是一個矩陣。旋轉矩陣是:

Cos(a) -Sin(a) 
Sin(a) Cos(a) 

其中'a' - 以弧度表示的旋轉角度。

這意味着你可以這樣做:

Visual v = this; // your Rotatable.MyRotatableControl class instance. 
Matrix m = PresentationSource.FromVisual(v).CompositionTarget.TransformToDevice; 
double rotationAngleInDegrees = Math.Acos(m.M11) * 180/Math.PI; 
2

你只需要檢查,如果角度是在範圍內,如果超出範圍,把它放回範圍:

如果角度以度,然後覈對360:

MyRotateTransform.Angle += e.DeltaManipulation.Rotation; 

while (MyRotateTransform.Angle > 360) 
{ 
    MyRotateTransform.Angle -= 360; 
} 

while (MyRotateTransform.Angle < 360) 
{ 
    MyRotateTransform.Angle += 360; 
} 

如果角度弧度,你需要檢查對2 * PI:

MyRotateTransform.Angle += e.DeltaManipulation.Rotation; 

while (MyRotateTransform.Angle > 2 * Math.PI) 
{ 
    MyRotateTransform.Angle -= 2 * Math.PI; 
} 

while (MyRotateTransform.Angle < 2 * Math.PI) 
{ 
    MyRotateTransform.Angle += 2 * Math.PI; 
} 

只要你的輪換不是很大,就不需要循環,但你永遠不會知道。

+0

「角度以弧度表示」 - 不,它們不是:https://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform.angle(v=vs.110) .aspx – 2017-05-24 07:45:46

+0

@JimBalter - 嗯。當我寫這篇文章時,我會發誓說他們是用弧度表示的。我已經檢查過以前的.NET版本,看起來從.NET 3(這是文檔發佈的距離),它清楚地表明瞭「度數」。 – ChrisF 2017-05-24 08:09:30

+0

有沒有必要檢查回來。這將是對API的瘋狂無聲的重大改變。 – 2017-05-24 17:12:30