/
SwipeDirection.cs
52 lines (44 loc) · 1.19 KB
/
SwipeDirection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.ComponentModel;
namespace Xamarin.Forms
{
[Flags]
public enum SwipeDirection
{
Right = 1,
Left = 2,
Up = 4,
Down = 8
}
}
namespace Xamarin.Forms.Internals
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static class SwipeDirectionHelper
{
public static SwipeDirection GetSwipeDirection(Point initialPoint, Point endPoint)
{
var angle = GetAngleFromPoints(initialPoint.X, initialPoint.Y, endPoint.X, endPoint.Y);
return GetSwipeDirectionFromAngle(angle);
}
internal static double GetAngleFromPoints(double x1, double y1, double x2, double y2)
{
double rad = Math.Atan2(y1 - y2, x2 - x1) + Math.PI;
return (rad * 180 / Math.PI + 180) % 360;
}
internal static SwipeDirection GetSwipeDirectionFromAngle(double angle)
{
if (IsAngleInRange(angle, 45, 135))
return SwipeDirection.Up;
if (IsAngleInRange(angle, 0, 45) || IsAngleInRange(angle, 315, 360))
return SwipeDirection.Right;
if (IsAngleInRange(angle, 225, 315))
return SwipeDirection.Down;
return SwipeDirection.Left;
}
internal static bool IsAngleInRange(double angle, float init, float end)
{
return (angle >= init) && (angle < end);
}
}
}