Skip to content
This repository was archived by the owner on Apr 10, 2020. It is now read-only.

Commit

Permalink
Fix FlipView issue where a fast flick might cause a non-animated navi…
Browse files Browse the repository at this point in the history
…gation
  • Loading branch information
Kinnara committed Feb 10, 2014
1 parent 5d4dff4 commit ecb862a
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions Microsoft.Phone.Controls.Toolkit/FlipView/FlipView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class FlipView : TemplatedItemsControl<FlipViewItem>, ISupportInitialize
private bool _loaded;

private bool _animating;
private bool _isEffectiveDragging;
private bool _hasDragDelta;
private bool _dragging;
private DragLock _dragLock;
private WeakReference _gestureSource;
Expand Down Expand Up @@ -653,7 +653,7 @@ private void UpdateSelection(int oldSelectedIndex, int newSelectedIndex, object
{
AnimateTo(SelectedIndex, false);
}
else if (!_isEffectiveDragging)
else if (!_hasDragDelta)
{
if (_animating)
{
Expand Down Expand Up @@ -800,58 +800,59 @@ internal void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)

e.Handled = true;

if (_dragLock == DragLock.Horizontal && e.DeltaManipulation.Translation.X != 0 && Orientation == Orientation.Horizontal ||
_dragLock == DragLock.Vertical && e.DeltaManipulation.Translation.Y != 0 && Orientation == Orientation.Vertical)
if (HasDragDelta(e.DeltaManipulation))
{
Drag(e);
}
}

internal void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
_hasDragDelta = HasDragDelta(e.TotalManipulation);

ManipulationDelta totalManipulation = null;

_gestureStartedEventArgs = null;
_dragLock = DragLock.Unset;
_dragging = false;

if (e.IsInertial)
if (_hasDragDelta)
{
double angle = AngleFromVector(e.FinalVelocities.LinearVelocity.X, e.FinalVelocities.LinearVelocity.Y);

if (Orientation == Orientation.Vertical)
if (e.IsInertial)
{
angle -= 90;
if (angle < 0)
double angle = AngleFromVector(e.FinalVelocities.LinearVelocity.X, e.FinalVelocities.LinearVelocity.Y);

if (Orientation == Orientation.Vertical)
{
angle += 360;
angle -= 90;
if (angle < 0)
{
angle += 360;
}
}
}

if (angle <= 45 || angle >= 315)
{
angle = 0;
}
else if (angle >= 135 && angle <= 225)
{
angle = 180;
}
if (angle <= 45 || angle >= 315)
{
angle = 0;
}
else if (angle >= 135 && angle <= 225)
{
angle = 180;
}

ReleaseMouseCaptureAtGestureOrigin();
ReleaseMouseCaptureAtGestureOrigin();

Flick(angle);
Flick(angle);

if (angle == 0 || angle == 180)
{
e.Handled = true;
if (angle == 0 || angle == 180)
{
e.Handled = true;
}
}
}
else if (e.TotalManipulation.Translation.X != 0 || e.TotalManipulation.Translation.Y != 0)
{
totalManipulation = e.TotalManipulation;

if (_isEffectiveDragging)
else
{
totalManipulation = e.TotalManipulation;

e.Handled = true;
}
}
Expand All @@ -861,7 +862,7 @@ internal void OnManipulationCompleted(object sender, ManipulationCompletedEventA

private void Drag(ManipulationDeltaEventArgs e)
{
_isEffectiveDragging = true;
_hasDragDelta = HasDragDelta(e.CumulativeManipulation);

if (!ShouldHandleManipulation)
{
Expand Down Expand Up @@ -936,19 +937,19 @@ private void Flick(double angle)

private void GesturesComplete(ManipulationDelta totalManipulation)
{
if (ShouldHandleManipulation)
if (ShouldHandleManipulation && _hasDragDelta)
{
if (totalManipulation != null && _isEffectiveDragging)
if (totalManipulation != null)
{
AnimateTo((int)Math.Round(ScrollOffset - TransformOffset / ItemSize));
}
else if (totalManipulation == null && !_animating)
else if (!_animating)
{
AnimateTo(EffectiveSelectedIndex);
}
}

_isEffectiveDragging = false;
_hasDragDelta = false;
_offsetWhenDragStarted = null;
_squishing = false;
}
Expand All @@ -975,6 +976,12 @@ private void ReleaseMouseCaptureAtGestureOrigin()
}
}

private bool HasDragDelta(ManipulationDelta manipulation)
{
return _dragLock == DragLock.Horizontal && manipulation.Translation.X != 0 && Orientation == Orientation.Horizontal ||
_dragLock == DragLock.Vertical && manipulation.Translation.Y != 0 && Orientation == Orientation.Vertical;
}

private void AnimateTo(int index, bool changeIndex = true)
{
if (_suppressAnimation)
Expand Down

0 comments on commit ecb862a

Please sign in to comment.