From 8b08801ef6ce3da92c4e5021f8d42a67a4f0591b Mon Sep 17 00:00:00 2001 From: Elte Hupkes Date: Wed, 28 Feb 2018 12:12:04 +0100 Subject: [PATCH] Provides a solution for #82 --- Xamarin-Sidebar/Sidebar.cs | 17 ++++++++++--- Xamarin-Sidebar/SidebarContentArea.cs | 35 ++++++++++++++++----------- Xamarin-Sidebar/SidebarController.cs | 11 ++++++--- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Xamarin-Sidebar/Sidebar.cs b/Xamarin-Sidebar/Sidebar.cs index 04ff22b..9a1d098 100644 --- a/Xamarin-Sidebar/Sidebar.cs +++ b/Xamarin-Sidebar/Sidebar.cs @@ -272,7 +272,7 @@ private void SetupGestureRecognizers() { TapGesture.AddTarget (() => CloseMenu()); TapGesture.NumberOfTapsRequired = 1; PanGesture = new UIPanGestureRecognizer { - Delegate = new SlideoutPanDelegate(), + Delegate = new SlideoutPanDelegate(this), MaximumNumberOfTouches = 1, MinimumNumberOfTouches = 1 }; @@ -280,11 +280,22 @@ private void SetupGestureRecognizers() { } + /// + /// Default pan gesture delegate used to start menu sliding + /// when appropriate. + /// private class SlideoutPanDelegate : UIGestureRecognizerDelegate { - public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch) + private readonly Sidebar _sidebar; + + public SlideoutPanDelegate(Sidebar sidebar) + { + _sidebar = sidebar; + } + + public override bool ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch) { - return true; + return _sidebar._sidebarContentArea.TouchInActiveArea(touch, _sidebar); } } } diff --git a/Xamarin-Sidebar/SidebarContentArea.cs b/Xamarin-Sidebar/SidebarContentArea.cs index 1e7e33c..a38367b 100644 --- a/Xamarin-Sidebar/SidebarContentArea.cs +++ b/Xamarin-Sidebar/SidebarContentArea.cs @@ -24,7 +24,6 @@ namespace SidebarNavigation { internal class SidebarContentArea { - private bool _ignorePan; private nfloat _panOriginX; private UIView _viewOverlay; @@ -121,27 +120,35 @@ public void AfterCloseAnimation(UITapGestureRecognizer tapGesture) { public void Pan(Sidebar sidebar) { if (sidebar.PanGesture.State == UIGestureRecognizerState.Began) { - PanBegan(sidebar.PanGesture, sidebar.MenuLocation, sidebar.GestureActiveArea); - } else if (!_ignorePan && (sidebar.PanGesture.State == UIGestureRecognizerState.Changed)) { + PanBegan(); + } else if (sidebar.PanGesture.State == UIGestureRecognizerState.Changed) { PanChanged(sidebar); - } else if (!_ignorePan && (sidebar.PanGesture.State == UIGestureRecognizerState.Ended || - sidebar.PanGesture.State == UIGestureRecognizerState.Cancelled)) { + } else if (sidebar.PanGesture.State == UIGestureRecognizerState.Ended || + sidebar.PanGesture.State == UIGestureRecognizerState.Cancelled) { PanEnded(sidebar); } } + /// + /// Returns whether a certain touch is within the area that should + /// activate the pan gesture sliding out the menu. + /// + /// + /// + /// + public bool TouchInActiveArea(UITouch touch, Sidebar sidebar) + { + var view = ContentViewController.View; + var position = touch.LocationInView(view).X; + var area = sidebar.GestureActiveArea; - private void PanBegan(UIPanGestureRecognizer panGesture, MenuLocations menuLocation, nfloat gestureActiveArea) { - _panOriginX = ContentViewController.View.Frame.X; - _ignorePan = PanGestureInActiveArea(panGesture, menuLocation, gestureActiveArea); + return sidebar.MenuLocation == MenuLocations.Left ? + position < area : + position > (view.Bounds.Width - area); } - private bool PanGestureInActiveArea(UIPanGestureRecognizer panGesture, MenuLocations menuLocation, nfloat gestureActiveArea) { - var position = panGesture.LocationInView(ContentViewController.View).X; - if (menuLocation == MenuLocations.Left) - return position > gestureActiveArea; - else - return position < ContentViewController.View.Bounds.Width - gestureActiveArea; + private void PanBegan() { + _panOriginX = ContentViewController.View.Frame.X; } private void PanChanged(Sidebar sidebar) { diff --git a/Xamarin-Sidebar/SidebarController.cs b/Xamarin-Sidebar/SidebarController.cs index 9617a11..a1b6cd1 100644 --- a/Xamarin-Sidebar/SidebarController.cs +++ b/Xamarin-Sidebar/SidebarController.cs @@ -23,7 +23,7 @@ namespace SidebarNavigation { public class SidebarController : UIViewController { - private Sidebar _sidebar; + private readonly Sidebar _sidebar; /// @@ -42,7 +42,7 @@ public SidebarController(IntPtr handle) : base(handle) /// /// The view controller for the content area. /// - /// + /// /// The view controller for the side menu. /// public SidebarController( @@ -70,7 +70,6 @@ public SidebarController( /// public event EventHandler StateChangeHandler; - /// /// The view controller shown in the content area. /// @@ -82,7 +81,11 @@ public SidebarController( /// public UIViewController MenuAreaController { get { return _sidebar.MenuViewController; } } - + /// + /// Exposes the underlying sidebar object + /// + public Sidebar Sidebar => _sidebar; + /// /// Determines the percent of width to complete slide action. ///