这是一个小.,需要一些编码。 但我将给它一个尝试。
首先,我改变了你的XAML,以便我使用Canvas
,而不是你使用的<编码>Stacknel。 在这种情况下,我更喜欢<代码>Canvas,因为它使我能够使用绝对坐标定位旋转图像,以便我确切知道投票站的哪里。 XAML现在喜欢:
<UserControl x:Class="TurnTable.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
>
<Canvas x:Name="LayoutRoot" Background="White">
<!-- Disk rotating code -->
<Canvas.Resources>
<Storyboard x:Name="myStoryboard">
<DoubleAnimation x:Name="angleAnimation" Storyboard.TargetName="myTransform"
Storyboard.TargetProperty="Angle"
From="0" To="0" Duration="0:0:1"
>
<DoubleAnimation.EasingFunction>
<CubicEase />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</Canvas.Resources>
<Rectangle x:Name="ttbg" Canvas.Left="100" Canvas.Top="100" Stroke="Black" StrokeThickness="0" RenderTransformOrigin="0.5,0.5" Height="420" Width="420" MouseLeftButtonDown="press_down" MouseLeftButtonUp="press_up" MouseMove="press_move" >
<Rectangle.Fill>
<ImageBrush ImageSource="Images/ttbg.png" Stretch="Uniform" />
</Rectangle.Fill>
<Rectangle.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="myTransform" />
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</UserControl>
Also note that I gave the storyboard a duration of 1 second and I gave it an EasingFunction
to give the animation a nice look and feel.
在用户控制编码中,我正在使用Math.Atan
功能,以计算将 turn改性运动的角。 我正在使用与周转房中心相对的模拟坐标来确定向<代码>Math.Atan提供食物的价值。 守则最后设想如下:
namespace TurnTable
{
public partial class MainPage : UserControl
{
Point _centerOfTurnTable;
Point _startMousePosition;
Point _lastMousePosition;
bool _isMouseCaptured = false;
double _deltaAngle;
public MainPage()
{
InitializeComponent();
}
private void press_down(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_centerOfTurnTable = new Point
(
Canvas.GetLeft(ttbg) + ttbg.ActualWidth / 2d,
Canvas.GetTop(ttbg) + ttbg.ActualHeight / 2d
);
myStoryboard.Pause();
_startMousePosition = e.GetPosition(this);
_isMouseCaptured = true;
_lastMousePosition = _startMousePosition;
}
private void press_up(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (_isMouseCaptured)
{
_isMouseCaptured = false;
angleAnimation.From = myTransform.Angle;
angleAnimation.To = myTransform.Angle + _deltaAngle * 100;
_deltaAngle = 0;
myStoryboard.Begin();
}
}
private void press_move(object sender, System.Windows.Input.MouseEventArgs e)
{
Point currentMousePosition = e.GetPosition(this);
if (_isMouseCaptured && Math.Abs(currentMousePosition.X - _centerOfTurnTable.X) > 5)
{
_deltaAngle = GetAngleDelta(currentMousePosition);
myTransform.Angle += _deltaAngle;
_lastMousePosition = currentMousePosition;
}
}
private double GetAngleDelta(Point currentMousePosition)
{
double lastAngleDegrees = GetAngleDegrees(_lastMousePosition);
double newAngleDegrees = GetAngleDegrees(currentMousePosition);
if (Math.Sign(lastAngleDegrees) != Math.Sign(newAngleDegrees))
{
lastAngleDegrees = newAngleDegrees;
}
double delta = newAngleDegrees - lastAngleDegrees;
return delta;
}
private double GetAngleDegrees(Point position)
{
return 180d / Math.PI * Math.Atan((position.Y - _centerOfTurnTable.Y) / (position.X - _centerOfTurnTable.X));
}
}
}
The code allows you to turn the turntable and when depressing the mouse an animation will be started to allow the turntable to slowly come to a complete stop which gives it a nice touch.