summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/PinchGestureHandler.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 13:02:25 -0700
committerJason Smith <jason.smith@xamarin.com>2016-03-22 16:13:41 -0700
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.Android/PinchGestureHandler.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.Android/PinchGestureHandler.cs')
-rw-r--r--Xamarin.Forms.Platform.Android/PinchGestureHandler.cs76
1 files changed, 76 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/PinchGestureHandler.cs b/Xamarin.Forms.Platform.Android/PinchGestureHandler.cs
new file mode 100644
index 00000000..bc06531c
--- /dev/null
+++ b/Xamarin.Forms.Platform.Android/PinchGestureHandler.cs
@@ -0,0 +1,76 @@
+using System;
+using System.Linq;
+
+namespace Xamarin.Forms.Platform.Android
+{
+ internal class PinchGestureHandler
+ {
+ double _pinchStartingScale = 1;
+
+ public PinchGestureHandler(Func<View> getView)
+ {
+ GetView = getView;
+ }
+
+ public bool IsPinchSupported
+ {
+ get
+ {
+ View view = GetView();
+ return view != null && view.GestureRecognizers.GetGesturesFor<PinchGestureRecognizer>().Any();
+ }
+ }
+
+ Func<View> GetView { get; }
+
+ // A View can have at most one pinch gesture, so we just need to look for one (or none)
+ PinchGestureRecognizer PinchGesture => GetView()?.GestureRecognizers.GetGesturesFor<PinchGestureRecognizer>().FirstOrDefault();
+
+ public bool OnPinch(float scale, Point scalePoint)
+ {
+ View view = GetView();
+
+ if (view == null)
+ return false;
+
+ PinchGestureRecognizer pinchGesture = PinchGesture;
+ if (pinchGesture == null)
+ return true;
+
+ var scalePointTransformed = new Point(scalePoint.X / view.Width, scalePoint.Y / view.Height);
+ ((IPinchGestureController)pinchGesture).SendPinch(view, 1 + (scale - 1) * _pinchStartingScale, scalePointTransformed);
+
+ return true;
+ }
+
+ public void OnPinchEnded()
+ {
+ View view = GetView();
+
+ if (view == null)
+ return;
+
+ PinchGestureRecognizer pinchGesture = PinchGesture;
+ ((IPinchGestureController)pinchGesture)?.SendPinchEnded(view);
+ }
+
+ public bool OnPinchStarted(Point scalePoint)
+ {
+ View view = GetView();
+
+ if (view == null)
+ return false;
+
+ PinchGestureRecognizer pinchGesture = PinchGesture;
+ if (pinchGesture == null)
+ return false;
+
+ _pinchStartingScale = view.Scale;
+
+ var scalePointTransformed = new Point(scalePoint.X / view.Width, scalePoint.Y / view.Height);
+
+ ((IPinchGestureController)pinchGesture).SendPinchStarted(view, scalePointTransformed);
+ return true;
+ }
+ }
+} \ No newline at end of file