标准化 WPF 操作旋转的角度

发布于 2024-11-02 20:47:57 字数 3346 浏览 0 评论 0原文

我发现了这个很好的例子,而且效果很好。 我的数学很差,所以用这种方法旋转和增加惯性真的很容易。 我的问题是我需要将旋转角度标准化为 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));
        }
    }
}

I found this nice example, and it works perfectly.
I am really bad with maths, so rotating and adding inertia is really easy this way.
My problem is that I need to normalize rotation angle to 0 to 360.
So after complete one spin in one direction to other I can pick value from 0 to 360, clockwise or 360 to 0 (anti-clockwise)
I would appreciate it if somebody could help me with this.

<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));
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2024-11-09 20:47:57

您只需要检查角度是否在范围内,如果超出范围,请将其恢复到范围内:

如果角度以度为单位,则检查 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;
}

只要你的旋转不大,就不需要循环,但你永远不知道。

You just need to check that if the angle is in range and if it goes out of range, bring it back into range:

If the angle is in degrees, then check against 360:

MyRotateTransform.Angle += e.DeltaManipulation.Rotation;

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

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

If angles are in radians, you need to check against 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;
}

As long as your rotation isn't large there shouldn't really be a need to loop, but you never know.

悲喜皆因你 2024-11-09 20:47:57

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;

Any transform in WPF - it's a Matrix. A rotation matrix is:

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

where 'a' - the rotation angle in radians.

That means you can do:

Visual v = this; // your Rotatable.MyRotatableControl class instance.
Matrix m = PresentationSource.FromVisual(v).CompositionTarget.TransformToDevice;
double rotationAngleInDegrees = Math.Acos(m.M11) * 180 / Math.PI;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文