summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/GlobalCloseContextGestureRecognizer.cs
blob: 1a4e9c044261af719db32b3b4dfbf7a09074a1e3 (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
using Foundation;
using UIKit;
using NSAction = System.Action;
using RectangleF = CoreGraphics.CGRect;

namespace Xamarin.Forms.Platform.iOS
{
	internal class GlobalCloseContextGestureRecognizer : UIGestureRecognizer
	{
		UIScrollView _scrollView;

		public GlobalCloseContextGestureRecognizer(UIScrollView scrollView, NSAction activated) : base(activated)
		{
			_scrollView = scrollView;

			ShouldReceiveTouch = OnShouldReceiveTouch;
		}

		public override void TouchesBegan(NSSet touches, UIEvent evt)
		{
			State = UIGestureRecognizerState.Began;
			base.TouchesBegan(touches, evt);
		}

		public override void TouchesEnded(NSSet touches, UIEvent evt)
		{
			State = UIGestureRecognizerState.Recognized;
			base.TouchesEnded(touches, evt);
		}

		public override void TouchesMoved(NSSet touches, UIEvent evt)
		{
			State = UIGestureRecognizerState.Recognized;
			base.TouchesMoved(touches, evt);
		}

		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);

			if (disposing)
			{
				_scrollView = null;
			}
		}

		bool OnShouldReceiveTouch(UIGestureRecognizer r, UITouch t)
		{
			var scrollPos = t.LocationInView(_scrollView);
			var rect = new RectangleF(0, 0, _scrollView.ContentSize.Width, _scrollView.ContentSize.Height);
			return !rect.Contains(scrollPos);
		}
	}
}