我發現這個很好的例子,它完美的作品。 我對數學非常不滿,所以旋轉和增加慣性非常容易。 我的問題是,我需要正常化旋轉角度爲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));
}
}
}
「角度以弧度表示」 - 不,它們不是:https://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform.angle(v=vs.110) .aspx – 2017-05-24 07:45:46
@JimBalter - 嗯。當我寫這篇文章時,我會發誓說他們是用弧度表示的。我已經檢查過以前的.NET版本,看起來從.NET 3(這是文檔發佈的距離),它清楚地表明瞭「度數」。 – ChrisF 2017-05-24 08:09:30
有沒有必要檢查回來。這將是對API的瘋狂無聲的重大改變。 – 2017-05-24 17:12:30