summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/PinchGestureHandler.cs
blob: e5d3137368269a56b9c5ab925134f062a0db5186 (plain)
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
using System;
using System.Linq;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.Android
{
	internal class PinchGestureHandler
	{
		double _pinchStartingScale = 1;

		public PinchGestureHandler(Func<View> getView)
		{
			GetView = getView;
		}

		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.OfType<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);
			pinchGesture.SendPinch(view, 1 + (scale - 1) * _pinchStartingScale, scalePointTransformed);

			return true;
		}

		public void OnPinchEnded()
		{
			View view = GetView();

			if (view == null)
				return;

			PinchGestureRecognizer pinchGesture = PinchGesture;
			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);

			pinchGesture.SendPinchStarted(view, scalePointTransformed);
			return true;
		}
	}
}