-
Notifications
You must be signed in to change notification settings - Fork 17
/
DraggableViewRenderer.cs
201 lines (174 loc) · 7.38 KB
/
DraggableViewRenderer.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using System.ComponentModel;
using CoreGraphics;
using Xamarin.Forms.RadialMenu;
using Xamarin.Forms.RadialMenu.iOSCore;
using static Xamarin.Forms.RadialMenu.RadialMenu;
using System;
[assembly: ExportRenderer(typeof(RadialMenu), typeof(DraggableViewRenderer))]
namespace Xamarin.Forms.RadialMenu.iOSCore
{
public class DraggableViewRenderer : VisualElementRenderer<View>
{
bool longPress = false;
bool firstTime = true;
double lastTimeStamp = 0f;
UIPanGestureRecognizer panGesture;
CGPoint lastLocation;
CGPoint originalPosition;
UIGestureRecognizer.Token panGestureToken;
CGRect displayMetrics;
nfloat sH, sW;
bool isFixingfMenuPosition = false;
void DetectPan()
{
var dragView = Element as RadialMenu;
var ne=Xamarin.Forms.Platform.iOS.Platform.GetRenderer(Element).NativeView;
if (longPress || dragView.DragMode == RadialMenu.DragMod.Touch)
{
if (panGesture.State == UIGestureRecognizerState.Began)
{
dragView.DragStarted();
if (firstTime)
{
originalPosition = Center;
firstTime = false;
}
}
CGPoint translation = panGesture.TranslationInView(Superview);
var currentCenterX = Center.X;
var currentCenterY = Center.Y;
if (dragView.DragDirection == DragDirectionType.All || dragView.DragDirection == DragDirectionType.Horizontal)
{
currentCenterX = lastLocation.X + translation.X;
}
if (dragView.DragDirection == DragDirectionType.All || dragView.DragDirection == DragDirectionType.Vertical)
{
currentCenterY = lastLocation.Y + translation.Y;
}
if (((currentCenterX >= 30 && currentCenterX <= sW-30))&&((currentCenterY >= 30 && currentCenterY <= sH - 30)))
Center = new CGPoint(currentCenterX, currentCenterY);
if (panGesture.State == UIGestureRecognizerState.Ended)
{
dragView.DragEnded();
longPress = false;
lastLocation = Center;
}
}
}
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
RemoveGestureRecognizer(panGesture);
panGesture.RemoveTarget(panGestureToken);
}
if (e.NewElement != null)
{
displayMetrics = UIScreen.MainScreen.Bounds;
sH = displayMetrics.Height;
sW = displayMetrics.Width;
var dragView = Element as RadialMenu;
panGesture = new UIPanGestureRecognizer();
panGestureToken = panGesture.AddTarget(DetectPan);
AddGestureRecognizer(panGesture);
dragView.RestorePositionCommand = new Command(() =>
{
if (!firstTime)
{
Center = originalPosition;
}
});
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var dragView = Element as RadialMenu;
if (e.PropertyName == "IsOpened")
{
if (dragView.IsOpened)
{
CGPoint translation = panGesture.TranslationInView(Superview);
var currentCenterX = Center.X;
var currentCenterY = Center.Y;
int axisAdditionX = 100;
int axisAdditionY = 125;
if (dragView.IsMenuSandboxEnabled)
{
//SMART CONTAINMENT LOGIC
//Is on the left side?
if (currentCenterX <= axisAdditionX)
{
currentCenterX += (axisAdditionX - currentCenterX);
//Upper Y axis
if (currentCenterY <= axisAdditionY)
{
currentCenterY += (axisAdditionY - currentCenterY);
}
//Bottom Y Axis
if ((currentCenterY + axisAdditionY) >= sH)
{
currentCenterY = (sH - axisAdditionY);
}
Center = new CGPoint(currentCenterX, currentCenterY);
}
//Left X is good but Y top is not
if (currentCenterY <= axisAdditionY)
{
currentCenterY += (axisAdditionY - currentCenterY);
if (currentCenterX <= axisAdditionX)
{
currentCenterX += (axisAdditionX - currentCenterX);
}
Center = new CGPoint(currentCenterX, currentCenterY);
}
//Left X is good but Y bottom is not
if ((currentCenterY + axisAdditionY) >= sH)
{
currentCenterY = (sH - axisAdditionY);
Center = new CGPoint(currentCenterX, currentCenterY);
}
//Is on the right side?
if ((currentCenterX + axisAdditionX) >= sW)
{
currentCenterX = (sW - axisAdditionX);
//Upper Y axis
if (currentCenterY <= axisAdditionY)
{
currentCenterY += (axisAdditionY - currentCenterY);
}
//Bottom Y Axis
if ((currentCenterY + axisAdditionY) >= sH)
{
currentCenterY = (sH - axisAdditionY);
}
Center = new CGPoint(currentCenterX, currentCenterY);
}
lastLocation = Center;
}
}
}
base.OnElementPropertyChanged(sender, e);
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
lastTimeStamp = evt.Timestamp;
Superview.BringSubviewToFront(this);
lastLocation = Center;
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
if ((Element as RadialMenu).IsOpened) return;
if (evt.Timestamp - lastTimeStamp >= 0.5)
{
longPress = true;
}
base.TouchesMoved(touches, evt);
}
}
}