вращение wpf манипуляции - PullRequest
       2

вращение wpf манипуляции

0 голосов
/ 19 апреля 2011

Я нашел этот хороший пример, и он отлично работает.Я очень плохо разбираюсь в математике, поэтому вращать и добавлять инерцию очень легко.Моя проблема в том, что мне нужно нормализовать угол поворота от 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 ]

2 голосов
/ 20 апреля 2011

Вам просто нужно проверить, что если угол находится в диапазоне, и если он выходит за пределы диапазона, верните его в диапазон:

Если угол в градусах, то проверить по 360:

MyRotateTransform.Angle += e.DeltaManipulation.Rotation;

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

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

Если углы указаны в радианах, необходимо проверить по 2 * пи:

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

Пока ваше вращение не велико, на самом деле не должно быть необходимости в петле, но вы никогда не знаете.

1 голос
/ 20 апреля 2011

Любое преобразование в WPF - это матрица. Матрица вращения:

Cos(a) -Sin(a)
Sin(a)  Cos(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;
...