Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Parallax hack for fun and no profit

$
0
0

Hi all,

Was doing some hacking around with a customer who had some questions and we got to just thinking how to do Parallax in Xamarin.Forms. Custom renderer right? WRONG!

BOOM:

        var img = new Image () {
            Source = "https://c4.staticflickr.com/8/7372/16353550400_ce43734d93_b.jpg", 
            VerticalOptions = LayoutOptions.Start, 
            Scale = 2, 
            AnchorY = 0
        };

        var g = new Grid ();
        var s = new ScrollView ();
        s.Scrolled += (object sender, ScrolledEventArgs e) => {
            var imageHeight = img.Height * 2;
            var scrollRegion = g.Height - s.Height;
            var parallexRegion = imageHeight - s.Height;
            img.TranslationY = s.ScrollY - parallexRegion * (s.ScrollY / scrollRegion);
        };


        s.Content = g;

        g.Children.Add (img);

        StackLayout l = new StackLayout ();

        for (int i = 0; i < 50; i++)
        {
            l.Children.Add (new Label () { Text = "ewljrjwel weljrlkewjrw lkjrlkwejrlkwe", HeightRequest = 50 });
        }

        g.Children.Add (l);

        // The root page of your application    
        MainPage = new ContentPage {
            Content = s
        };

Only tested on iOS because you know this was just a fun hack. Everything named like crap because Darren is lazy and I made him type. Cheers.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>