Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biapar/7bc193df444814bf759d25aa8aee4125 to your computer and use it in GitHub Desktop.
Save biapar/7bc193df444814bf759d25aa8aee4125 to your computer and use it in GitHub Desktop.
Xamarin Forms Parallax Scrolling
public class DemoPage : ContentPage
{
public DemoPage ()
{
var image = new Image () {
Source = "http://eric.polerecky.com/images/abstract-1.jpg",
VerticalOptions = LayoutOptions.Start,
Scale = 3,
AnchorY = 0
};
var grid = new Grid ();
var scrollView = new ScrollView ();
scrollView.Scrolled += (object sender, ScrolledEventArgs e) => {
var imageHeight = image.Height * 3;
var scrollRegion = grid.Height - scrollView.Height;
var parallexRegion = imageHeight - scrollView.Height;
image.TranslationY = scrollView.ScrollY - parallexRegion * (scrollView.ScrollY / scrollRegion);
};
scrollView.Content = grid;
grid.Children.Add (image);
StackLayout stackLayout = new StackLayout () {
HorizontalOptions = LayoutOptions.Center
};
for (int i = 0; i < 50; i++) {
stackLayout.Children.Add (new Label () {
Text = "This is some text to scroll",
FontSize = 24,
HeightRequest = 50
});
}
grid.Children.Add (stackLayout);
this.Content = scrollView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment