diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.Android/PinchGestureHandler.cs | |
download | xamarin-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.cs | 76 |
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 |