Skip to content

Instantly share code, notes, and snippets.

@dkudelko
Created July 28, 2016 07:44
Show Gist options
  • Save dkudelko/dc3ceb386a8f3f7ad47580f043216804 to your computer and use it in GitHub Desktop.
Save dkudelko/dc3ceb386a8f3f7ad47580f043216804 to your computer and use it in GitHub Desktop.
image paralax effect
<ScrollView x:Name="scrollView" StyleId="scrollView">
<StackLayout VerticalOptions="Fill" BackgroundColor="Fuchsia">
<Image x:Name="photoImage" />
....
protected ovveride void OnAppearing()
{
scrollView.Scrolled += (sender, e) => Parallax();
Parallax();
...
}
void Parallax()
{
if(_imageHeight <= 0)
_imageHeight = photoImage.Height;
var y = scrollView.ScrollY * .4;
if(y >= 0)
{
//Move the Image's Y coordinate a fraction of the ScrollView's Y position
photoImage.Scale = 1;
photoImage.TranslationY = y;
}
else
{
//Calculate a scale that equalizes the height vs scroll
double newHeight = _imageHeight + (scrollView.ScrollY * -1);
photoImage.Scale = newHeight / _imageHeight;
photoImage.TranslationY = scrollView.ScrollY / 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment