Skip to content

Commit

Permalink
Allow card pages to animate in from any edge
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhardwicke committed May 3, 2017
1 parent 370bb0a commit c8d5cb2
Showing 1 changed file with 77 additions and 16 deletions.
93 changes: 77 additions & 16 deletions NControl.Controls/NControl.Controls/CardPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public enum CardPosition
Center
}

public enum EdgePosition
{
Left,
Right,
Top,
Bottom
}

/// <summary>
/// Card page. Based on custom transitions
/// </summary>
Expand Down Expand Up @@ -103,12 +111,6 @@ public CardPage()

_layout.Children.Add(_contentView, () => this.LayerPosition);

if (_platformHelper.ControlAnimatesItself)
{
_shadowLayer.TranslationY = _platformHelper.GetScreenSize().Height - (CardPadding.Top);
_contentView.TranslationY = _platformHelper.GetScreenSize().Height - (CardPadding.Top);
}

// Add tap
_overlay.GestureRecognizers.Add(new TapGestureRecognizer
{
Expand Down Expand Up @@ -251,6 +253,51 @@ public CardPosition Position
}
}

private EdgePosition _edge = EdgePosition.Bottom;
public EdgePosition Edge
{
get { return _edge; }
set
{
if (_edge == value) return;
_edge = value;

if (_platformHelper.ControlAnimatesItself)
{
switch (Edge)
{
case EdgePosition.Top:
_shadowLayer.TranslationX = 0;
_contentView.TranslationX = 0;
_shadowLayer.TranslationY = 0 - _platformHelper.GetScreenSize().Height + (CardPadding.Bottom);
_contentView.TranslationY = 0 - _platformHelper.GetScreenSize().Height + (CardPadding.Bottom);
break;
case EdgePosition.Bottom:
_shadowLayer.TranslationX = 0;
_contentView.TranslationX = 0;
_shadowLayer.TranslationY = _platformHelper.GetScreenSize().Height - (CardPadding.Top);
_contentView.TranslationY = _platformHelper.GetScreenSize().Height - (CardPadding.Top);
break;
case EdgePosition.Left:
_shadowLayer.TranslationX = 0 - _platformHelper.GetScreenSize().Width + (CardPadding.Right);
_contentView.TranslationX = 0 - _platformHelper.GetScreenSize().Width + (CardPadding.Right);
_shadowLayer.TranslationY = 0;
_contentView.TranslationY = 0;
break;
case EdgePosition.Right:
_shadowLayer.TranslationX = _platformHelper.GetScreenSize().Width - (CardPadding.Left);
_contentView.TranslationX = _platformHelper.GetScreenSize().Width - (CardPadding.Left);
_shadowLayer.TranslationY = 0;
_contentView.TranslationY = 0;
break;
default:
throw new ArgumentException(nameof(Edge));
}
}

}
}

private Thickness _cardPadding;
public Thickness CardPadding
{
Expand Down Expand Up @@ -292,18 +339,32 @@ public virtual Task ShowAsync()
public virtual async Task CloseAsync()
{
if (_platformHelper.ControlAnimatesItself) {
var tasks = new Task[3];
switch (Edge)
{
case EdgePosition.Top:
tasks[0] = _shadowLayer.TranslateTo(0.0, 0 - _platformHelper.GetScreenSize().Height + (CardPadding.Bottom), 250, Easing.CubicInOut);
tasks[1] = _contentView.TranslateTo(0.0, 0 - _platformHelper.GetScreenSize().Height + (CardPadding.Bottom), 250, Easing.CubicInOut);
break;
case EdgePosition.Bottom:
tasks[0] = _shadowLayer.TranslateTo(0.0, _platformHelper.GetScreenSize().Height - (CardPadding.Top), 250, Easing.CubicInOut);
tasks[1] = _contentView.TranslateTo(0.0, _platformHelper.GetScreenSize().Height - (CardPadding.Top), 250, Easing.CubicInOut);
break;
case EdgePosition.Left:
tasks[0] = _shadowLayer.TranslateTo(0 - _platformHelper.GetScreenSize().Width + (CardPadding.Right), 0.0, 250, Easing.CubicInOut);
tasks[1] = _contentView.TranslateTo(0 - _platformHelper.GetScreenSize().Width + (CardPadding.Right), 0.0, 250, Easing.CubicInOut);
break;
case EdgePosition.Right:
tasks[0] = _shadowLayer.TranslateTo(_platformHelper.GetScreenSize().Width - (CardPadding.Left), 0.0, 250, Easing.CubicInOut);
tasks[1] = _contentView.TranslateTo(_platformHelper.GetScreenSize().Width - (CardPadding.Left), 0.0, 250, Easing.CubicInOut);
break;
default:
throw new ArgumentException(nameof(Edge));
}

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
_shadowLayer.TranslateTo(0.0,

#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
_platformHelper.GetScreenSize().Height - (CardPadding.Top), 250, Easing.CubicInOut);

await _contentView.TranslateTo(0.0,
_platformHelper.GetScreenSize().Height - (CardPadding.Top), 250, Easing.CubicInOut);

await _overlay.FadeTo(0.0F, 150, Easing.CubicInOut);
tasks[2] = _overlay.FadeTo(0.0F, 150, Easing.CubicInOut);

await Task.WhenAll(tasks);
}

await _platformHelper.CloseAsync (this);
Expand Down

0 comments on commit c8d5cb2

Please sign in to comment.