Skip to content

Commit

Permalink
Fix an animation bug of NavigationViewList. (#188)
Browse files Browse the repository at this point in the history
*  Fix an animation bug of NavigationViewList.

* modify code to avoid /0 exception.
  • Loading branch information
HHChaos authored and licanhua committed Jan 22, 2019
1 parent a34e01c commit 45c7b67
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions dev/NavigationView/NavigationView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,8 @@ void NavigationView::AnimateSelectionChanged(const winrt::IInspectable& prevItem

winrt::Point prevPosPoint = prevIndicator.TransformToVisual(paneContentGrid).TransformPoint(point);
winrt::Point nextPosPoint = nextIndicator.TransformToVisual(paneContentGrid).TransformPoint(point);
winrt::Size prevSize = prevIndicator.RenderSize();
winrt::Size nextSize = nextIndicator.RenderSize();

if (IsTopNavigationView())
{
Expand All @@ -982,8 +984,8 @@ void NavigationView::AnimateSelectionChanged(const winrt::IInspectable& prevItem
winrt::CompositionScopedBatch scopedBatch = visual.Compositor().CreateScopedBatch(winrt::CompositionBatchTypes::Animation);

// Play the animation on both the previous and next indicators
PlayIndicatorAnimations(prevIndicator, 0, nextPos - prevPos, true);
PlayIndicatorAnimations(nextIndicator, prevPos - nextPos, 0, false);
PlayIndicatorAnimations(prevIndicator, 0, nextPos - prevPos, prevSize, nextSize, true);
PlayIndicatorAnimations(nextIndicator, prevPos - nextPos, 0, prevSize, nextSize, false);

scopedBatch.End();
m_prevIndicator.set(prevIndicator);
Expand Down Expand Up @@ -1016,14 +1018,22 @@ void NavigationView::AnimateSelectionChanged(const winrt::IInspectable& prevItem
}
}

void NavigationView::PlayIndicatorAnimations(const winrt::UIElement& indicator, float from, float to, bool isOutgoing)
void NavigationView::PlayIndicatorAnimations(const winrt::UIElement& indicator, float from, float to, winrt::Size beginSize, winrt::Size endSize, bool isOutgoing)
{
winrt::Visual visual = winrt::ElementCompositionPreview::GetElementVisual(indicator);
winrt::Compositor comp = visual.Compositor();

winrt::Size size = indicator.RenderSize();
float dimension = IsTopNavigationView() ? size.Width : size.Height;

float beginScale = 1.0f;
float endScale = 1.0f;
if (IsTopNavigationView() && fabs(size.Width) > 0.001f)
{
beginScale = beginSize.Width / size.Width;
endScale = endSize.Width / size.Width;
}

winrt::StepEasingFunction singleStep = comp.CreateStepEasingFunction();
singleStep.IsFinalStepSingleFrame(true);

Expand All @@ -1040,14 +1050,14 @@ void NavigationView::PlayIndicatorAnimations(const winrt::UIElement& indicator,
}

winrt::ScalarKeyFrameAnimation posAnim = comp.CreateScalarKeyFrameAnimation();
posAnim.InsertKeyFrame(0.0f, from);
posAnim.InsertKeyFrame(0.333f, to, singleStep);
posAnim.InsertKeyFrame(0.0f, from < to ? from : (from + (dimension * (beginScale - 1))));
posAnim.InsertKeyFrame(0.333f, from < to ? (to + (dimension * (endScale - 1))) : to, singleStep);
posAnim.Duration(600ms);

winrt::ScalarKeyFrameAnimation scaleAnim = comp.CreateScalarKeyFrameAnimation();
scaleAnim.InsertKeyFrame(0.0f, 1);
scaleAnim.InsertKeyFrame(0.333f, abs(to - from) / dimension + 1, comp.CreateCubicBezierEasingFunction(c_frame1point1, c_frame1point2));
scaleAnim.InsertKeyFrame(1.0f, 1, comp.CreateCubicBezierEasingFunction(c_frame2point1, c_frame2point2));
scaleAnim.InsertKeyFrame(0.0f, beginScale);
scaleAnim.InsertKeyFrame(0.333f, abs(to - from) / dimension + (from < to ? endScale : beginScale), comp.CreateCubicBezierEasingFunction(c_frame1point1, c_frame1point2));
scaleAnim.InsertKeyFrame(1.0f, endScale, comp.CreateCubicBezierEasingFunction(c_frame2point1, c_frame2point2));
scaleAnim.Duration(600ms);

winrt::ScalarKeyFrameAnimation centerAnim = comp.CreateScalarKeyFrameAnimation();
Expand Down
2 changes: 1 addition & 1 deletion dev/NavigationView/NavigationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class NavigationView :
void RaiseDisplayModeChanged(const winrt::NavigationViewDisplayMode& displayMode);
void AnimateSelectionChanged(const winrt::IInspectable& lastItem, const winrt::IInspectable& currentItem);
void AnimateSelectionChangedToItem(const winrt::IInspectable& selectedItem);
void PlayIndicatorAnimations(const winrt::UIElement& indicator, float yFrom, float yTo, bool isOutgoing);
void PlayIndicatorAnimations(const winrt::UIElement& indicator, float yFrom, float yTo, winrt::Size beginSize, winrt::Size endSize, bool isOutgoing);
void OnAnimationComplete(const winrt::IInspectable& sender, const winrt::CompositionBatchCompletedEventArgs& args);
void ResetElementAnimationProperties(const winrt::UIElement& element, float desiredOpacity);
winrt::NavigationViewItem NavigationViewItemOrSettingsContentFromData(const winrt::IInspectable& data);
Expand Down

0 comments on commit 45c7b67

Please sign in to comment.