我发现了这个很好的例子,它工作得很好。我数学真的很差,所以旋转和增加惯性真的很容易。我的问题是,我需要将旋转Angular 归一化为0到360。所以在一个方向完成一个旋转到另一个方向后,我可以从0到360选择值,顺时针或360到0(逆时针)如果有人能帮助我,我将不胜感激。
<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));
}
}
}
2条答案
按热度按时间hujrc8aj1#
您只需检查Angular 是否在范围内,如果超出范围,则将其带回范围内:
如果Angular 以度为单位,则检查360:
如果Angular 是弧度,你需要检查2 * pi:
只要你的旋转不是很大,就不应该真的需要循环,但你永远不知道。
acruukt92#
WPF中的任何变换-它是一个矩阵。旋转矩阵是:
其中‘a’-以弧度为单位的旋转Angular 。
这意味着您可以: