Created
July 28, 2016 07:44
-
-
Save dkudelko/dc3ceb386a8f3f7ad47580f043216804 to your computer and use it in GitHub Desktop.
image paralax effect
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
<ScrollView x:Name="scrollView" StyleId="scrollView"> | |
<StackLayout VerticalOptions="Fill" BackgroundColor="Fuchsia"> | |
<Image x:Name="photoImage" /> | |
.... |
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
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