-
Notifications
You must be signed in to change notification settings - Fork 724
fix: Update Scroll View Animation Example and Sample Code Presenter #1616
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
Merged
karkarl
merged 3 commits into
microsoft:main
from
Zakariathr22:scrollview-animation-sample-updates
Nov 13, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
WinUIGallery/ControlPagesSampleCode/ScrollView/ScrollViewSample3_AccordionAnimation_cs.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //Accordion Animation | ||
| private void ScrollView_ScrollAnimationStarting(ScrollView sender, ScrollingScrollAnimationStartingEventArgs e) | ||
| { | ||
| // Cast the animation from the event arguments to a Vector3KeyFrameAnimation. | ||
| Vector3KeyFrameAnimation stockKeyFrameAnimation = e.Animation as Vector3KeyFrameAnimation; | ||
|
|
||
| // Check if the animation is of the correct type. | ||
| if (stockKeyFrameAnimation != null) | ||
| { | ||
| // Calculate the target vertical offset for the scroll animation. | ||
| double targetVerticalOffset = GetTargetVerticalOffset(); | ||
| float targetVerticalPosition = (float)targetVerticalOffset; | ||
|
|
||
| // Create a new Vector3KeyFrameAnimation for custom animation. | ||
| Vector3KeyFrameAnimation customKeyFrameAnimation = stockKeyFrameAnimation.Compositor.CreateVector3KeyFrameAnimation(); | ||
|
|
||
| // Calculate the initial vertical offset change for the animation. | ||
| float deltaVerticalPosition = 0.1f * (float)(targetVerticalOffset - scrollView3.VerticalOffset); | ||
|
|
||
| // Define the animation steps with keyframes for a smooth transition. | ||
| for (int step = 0; step < 3; step++) | ||
| { | ||
| // Insert a keyframe at a specific time with a computed vertical position. | ||
| customKeyFrameAnimation.InsertKeyFrame( | ||
| 1.0f - (0.4f / (float)Math.Pow(2, step)), // Time progress for the keyframe | ||
| new Vector3((float)scrollView3.HorizontalOffset, targetVerticalPosition + deltaVerticalPosition, 0.0f)); | ||
|
|
||
| // Adjust the deltaVerticalPosition for the next keyframe to create an "accordion" effect. | ||
| deltaVerticalPosition /= -2.0f; | ||
| } | ||
|
|
||
| // Insert the final keyframe with the target vertical position. | ||
| customKeyFrameAnimation.InsertKeyFrame(1.0f, new Vector3((float)scrollView3.HorizontalOffset, targetVerticalPosition, 0.0f)); | ||
|
|
||
| // Set the duration of the custom animation. | ||
| customKeyFrameAnimation.Duration = TimeSpan.FromMilliseconds(nbAnimationDuration.Value); | ||
|
|
||
| // Replace the default animation with the custom animation. | ||
| e.Animation = customKeyFrameAnimation; | ||
| } | ||
| } | ||
|
|
||
| // This function is triggered when the button is clicked to scroll with animation. | ||
| private void BtnScrollWithAnimation_Click(object sender, RoutedEventArgs e) | ||
| { | ||
| // Check if the ScrollView control is initialized. | ||
| if (scrollView3 != null) | ||
| { | ||
| // Initiate a scroll to the target vertical offset with animation enabled. | ||
| scrollView3.ScrollTo(scrollView3.HorizontalOffset, GetTargetVerticalOffset(), new ScrollingScrollOptions(ScrollingAnimationMode.Enabled, ScrollingSnapPointsMode.Ignore)); | ||
| } | ||
| } | ||
|
|
||
| // This function calculates the target vertical offset based on the current vertical offset of the ScrollView. | ||
| private double GetTargetVerticalOffset() | ||
| { | ||
| // Determine if the current vertical offset is greater than half of the scrollable height. | ||
| if (scrollView3.VerticalOffset > scrollView3.ScrollableHeight / 2.0) | ||
| { | ||
| // If yes, return a lower target vertical offset. | ||
| return scrollView3.ScrollableHeight / 5.0; | ||
| } | ||
| else | ||
| { | ||
| // If no, return a higher target vertical offset. | ||
| return 4.0 * scrollView3.ScrollableHeight / 5.0; | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
WinUIGallery/ControlPagesSampleCode/ScrollView/ScrollViewSample3_DefaultAnimation_cs.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //Default Animation | ||
| private void ScrollView_ScrollAnimationStarting(ScrollView sender, ScrollingScrollAnimationStartingEventArgs e) | ||
| { | ||
| // Cast the animation from the event arguments to a Vector3KeyFrameAnimation. | ||
| Vector3KeyFrameAnimation stockKeyFrameAnimation = e.Animation as Vector3KeyFrameAnimation; | ||
|
|
||
| // Check if the animation is of the correct type. | ||
| if (stockKeyFrameAnimation != null) | ||
| { | ||
| // Set the duration of the default animation to the value specified by nbAnimationDuration. | ||
| stockKeyFrameAnimation.Duration = TimeSpan.FromMilliseconds(nbAnimationDuration.Value); | ||
| } | ||
| } | ||
|
|
||
| // This function is triggered when the button is clicked to scroll with animation. | ||
| private void BtnScrollWithAnimation_Click(object sender, RoutedEventArgs e) | ||
| { | ||
| // Check if the ScrollView control is initialized. | ||
| if (scrollView3 != null) | ||
| { | ||
| // Initiate a scroll to the target vertical offset with animation enabled. | ||
| scrollView3.ScrollTo(scrollView3.HorizontalOffset, GetTargetVerticalOffset(), new ScrollingScrollOptions(ScrollingAnimationMode.Enabled, ScrollingSnapPointsMode.Ignore)); | ||
| } | ||
| } | ||
|
|
||
| // This function calculates the target vertical offset based on the current vertical offset of the ScrollView. | ||
| private double GetTargetVerticalOffset() | ||
| { | ||
| // Determine if the current vertical offset is greater than half of the scrollable height. | ||
| if (scrollView3.VerticalOffset > scrollView3.ScrollableHeight / 2.0) | ||
| { | ||
| // If yes, return a lower target vertical offset. | ||
| return scrollView3.ScrollableHeight / 5.0; | ||
| } | ||
| else | ||
| { | ||
| // If no, return a higher target vertical offset. | ||
| return 4.0 * scrollView3.ScrollableHeight / 5.0; | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
...Gallery/ControlPagesSampleCode/ScrollView/ScrollViewSample3_TeleportationAnimation_cs.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| //Teleportation Animation | ||
| private void ScrollView_ScrollAnimationStarting(ScrollView sender, ScrollingScrollAnimationStartingEventArgs e) | ||
| { | ||
| // Cast the animation from the event arguments to a Vector3KeyFrameAnimation. | ||
| Vector3KeyFrameAnimation stockKeyFrameAnimation = e.Animation as Vector3KeyFrameAnimation; | ||
|
|
||
| // Check if the animation is of the correct type. | ||
| if (stockKeyFrameAnimation != null) | ||
| { | ||
| // Calculate the target vertical offset for the scroll animation. | ||
| double targetVerticalOffset = GetTargetVerticalOffset(); | ||
| float targetVerticalPosition = (float)targetVerticalOffset; | ||
|
|
||
| // Create a new Vector3KeyFrameAnimation for custom animation. | ||
| Vector3KeyFrameAnimation customKeyFrameAnimation = stockKeyFrameAnimation.Compositor.CreateVector3KeyFrameAnimation(); | ||
|
|
||
| // Calculate the difference between the current and target vertical positions. | ||
| float deltaVerticalPosition = (float)(targetVerticalOffset - scrollView3.VerticalOffset); | ||
|
|
||
| // Define easing functions for smooth transitions. | ||
| // Start easing function with cubic Bezier curve for a quick start. | ||
| CubicBezierEasingFunction cubicBezierStart = stockKeyFrameAnimation.Compositor.CreateCubicBezierEasingFunction( | ||
| new Vector2(1.0f, 0.0f), // Control point 1 | ||
| new Vector2(1.0f, 0.0f)); // Control point 2 | ||
|
|
||
| // Define step easing function for a sudden change in animation. | ||
| StepEasingFunction step = stockKeyFrameAnimation.Compositor.CreateStepEasingFunction(1); | ||
|
|
||
| // End easing function with cubic Bezier curve for a smooth end. | ||
| CubicBezierEasingFunction cubicBezierEnd = stockKeyFrameAnimation.Compositor.CreateCubicBezierEasingFunction( | ||
| new Vector2(0.0f, 1.0f), // Control point 1 | ||
| new Vector2(0.0f, 1.0f)); // Control point 2 | ||
|
|
||
| // Insert keyframes into the custom animation. | ||
| // First keyframe near the midpoint of the animation with a quick dip. | ||
| customKeyFrameAnimation.InsertKeyFrame( | ||
| 0.499999f, // Time progress for the keyframe (almost halfway) | ||
| new Vector3((float)scrollView3.HorizontalOffset, targetVerticalPosition - 0.9f * deltaVerticalPosition, 0.0f), | ||
| cubicBezierStart); // Easing function for start | ||
|
|
||
| // Second keyframe exactly at halfway with a sudden step change. | ||
| customKeyFrameAnimation.InsertKeyFrame( | ||
| 0.5f, // Time progress for the keyframe (exactly halfway) | ||
| new Vector3((float)scrollView3.HorizontalOffset, targetVerticalPosition - 0.1f * deltaVerticalPosition, 0.0f), | ||
| step); // Easing function for sudden change | ||
|
|
||
| // Final keyframe at the end of the animation. | ||
| customKeyFrameAnimation.InsertKeyFrame( | ||
| 1.0f, // Time progress for the keyframe (end) | ||
| new Vector3((float)scrollView3.HorizontalOffset, targetVerticalPosition, 0.0f), | ||
| cubicBezierEnd); // Easing function for end | ||
|
|
||
| // Set the duration of the custom animation. | ||
| customKeyFrameAnimation.Duration = TimeSpan.FromMilliseconds(nbAnimationDuration.Value); | ||
|
|
||
| // Replace the default animation with the custom animation. | ||
| e.Animation = customKeyFrameAnimation; | ||
| } | ||
| } | ||
|
|
||
| // This function is triggered when the button is clicked to scroll with animation. | ||
| private void BtnScrollWithAnimation_Click(object sender, RoutedEventArgs e) | ||
| { | ||
| // Check if the ScrollView control is initialized. | ||
| if (scrollView3 != null) | ||
| { | ||
| // Initiate a scroll to the target vertical offset with animation enabled. | ||
| scrollView3.ScrollTo(scrollView3.HorizontalOffset, GetTargetVerticalOffset(), new ScrollingScrollOptions(ScrollingAnimationMode.Enabled, ScrollingSnapPointsMode.Ignore)); | ||
| } | ||
| } | ||
|
|
||
| // This function calculates the target vertical offset based on the current vertical offset of the ScrollView. | ||
| private double GetTargetVerticalOffset() | ||
| { | ||
| // Determine if the current vertical offset is greater than half of the scrollable height. | ||
| if (scrollView3.VerticalOffset > scrollView3.ScrollableHeight / 2.0) | ||
| { | ||
| // If yes, return a lower target vertical offset. | ||
| return scrollView3.ScrollableHeight / 5.0; | ||
| } | ||
| else | ||
| { | ||
| // If no, return a higher target vertical offset. | ||
| return 4.0 * scrollView3.ScrollableHeight / 5.0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed indentation