Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provides a solution for #82 #83

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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