Skip to content

Commit

Permalink
Merge pull request #83 from ElteHupkes/ISSUE-82-customizable-pan-gesture
Browse files Browse the repository at this point in the history
Provides a solution for #82
  • Loading branch information
jdehlin authored Feb 28, 2018
2 parents 861b3fa + 8b08801 commit 683451c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
17 changes: 14 additions & 3 deletions Xamarin-Sidebar/Sidebar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,30 @@ private void SetupGestureRecognizers() {
TapGesture.AddTarget (() => CloseMenu());
TapGesture.NumberOfTapsRequired = 1;
PanGesture = new UIPanGestureRecognizer {
Delegate = new SlideoutPanDelegate(),
Delegate = new SlideoutPanDelegate(this),
MaximumNumberOfTouches = 1,
MinimumNumberOfTouches = 1
};
PanGesture.AddTarget(() => Pan());
}


/// <summary>
/// Default pan gesture delegate used to start menu sliding
/// when appropriate.
/// </summary>
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);
}
}
}
Expand Down
35 changes: 21 additions & 14 deletions Xamarin-Sidebar/SidebarContentArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace SidebarNavigation
{
internal class SidebarContentArea
{
private bool _ignorePan;
private nfloat _panOriginX;
private UIView _viewOverlay;

Expand Down Expand Up @@ -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);
}
}

/// <summary>
/// Returns whether a certain touch is within the area that should
/// activate the pan gesture sliding out the menu.
/// </summary>
/// <param name="touch"></param>
/// <param name="sidebar"></param>
/// <returns></returns>
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) {
Expand Down
11 changes: 7 additions & 4 deletions Xamarin-Sidebar/SidebarController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace SidebarNavigation
{
public class SidebarController : UIViewController {

private Sidebar _sidebar;
private readonly Sidebar _sidebar;


/// <summary>
Expand All @@ -42,7 +42,7 @@ public SidebarController(IntPtr handle) : base(handle)
/// <param name="contentViewController">
/// The view controller for the content area.
/// </param>
/// <param name="navigationViewController">
/// <param name="menuViewController">
/// The view controller for the side menu.
/// </param>
public SidebarController(
Expand Down Expand Up @@ -70,7 +70,6 @@ public SidebarController(
/// </summary>
public event EventHandler<bool> StateChangeHandler;


/// <summary>
/// The view controller shown in the content area.
/// </summary>
Expand All @@ -82,7 +81,11 @@ public SidebarController(
/// </summary>
public UIViewController MenuAreaController { get { return _sidebar.MenuViewController; } }


/// <summary>
/// Exposes the underlying sidebar object
/// </summary>
public Sidebar Sidebar => _sidebar;

/// <summary>
/// Determines the percent of width to complete slide action.
/// </summary>
Expand Down

0 comments on commit 683451c

Please sign in to comment.