KamielDev
KamielDev

Reputation: 569

How do I fade out the contents of a scrollview in Xamarin Forms iOS?

I built a custom scrollview renderer on android which fades out my text on the bottom.

how do i do the same for iOS? I cant figure it out. I know how to implement the renderer, but not how to make it fade

I added an empty FadeScrollView class to my main xamarin project.

public class FadeScrollView : ScrollView
{

}

I then load this scrollview renderer in my xaml page.

<ContentPage ...
             xmlns:customRenders="clr-namespace:MyNameSpace;assembly=MyNameSpace"
             >
    ...
    <customRenderers:FadeScrollView>
        ....
    </FadeScrollView>

</ContentPage>

I then set up the following custom renderer for android

[assembly: ExportRenderer(typeof(FadeScrollView), typeof(FadeScrollViewRendererAndroid))]
namespace MyNameSpace
{
    public class FadeScrollViewRendererAndroid : ScrollViewRenderer
    {
        public FadeScrollViewRendererAndroid(Context context) : base(context)
        {
            this.SetFadingEdgeLength(300);
            this.VerticalFadingEdgeEnabled = true;
        }
    }
}

And this results in an expected outcome:

Working fade scroll view renderer

Now I set up my iOS renderer in a similar way, but I'm not sure how to pull off the fade effect, and when I googled around I only found people showing you how to do it in swift

[assembly: ExportRenderer(typeof(FadeScrollView), typeof(FadeScrollViewRendererIOS))]
namespace MyNameSpace
{
    class FadeScrollViewRendererIOS : UIScrollView
    {
        public FadeScrollViewRendererIOS()
        {
            //fade out 
        }
    }
}

Upvotes: 0

Views: 629

Answers (1)

ColeX
ColeX

Reputation: 14475

It seems iOS does not provide the api like VerticalFadingEdgeEnabled in Android .

We can implement this in custom renderer with a gradient layer .

Try the code below .

[assembly: ExportRenderer(typeof(ScrollView), typeof(MyRenderer))]
namespace FormsApp.iOS
{

    class MyRenderer : ScrollViewRenderer
    {

        public override void LayoutSubviews()
        {
            base.LayoutSubviews();

            gradient.Frame = this.Bounds;
        }

        CAGradientLayer gradient = new CAGradientLayer();
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);


            if(e.NewElement != null)
            {
                gradient.Frame = this.Bounds;
                gradient.Colors = new CGColor[] {UIColor.Clear.CGColor, UIColor.Black.CGColor,
                    UIColor.Black.CGColor, UIColor.Clear.CGColor };
                gradient.Locations = new NSNumber[] {0.0,0.0,0.7,1 };
                Layer.Mask = gradient;

                this.Scrolled += (sender,ee)=> {

                    gradient.Frame = new CGRect(0, ContentOffset.Y, Bounds.Width, Bounds.Height);

                    if (ContentOffset.Y >= ContentSize.Height - Bounds.Height)
                    {
                        Layer.Mask = null;
                    }
                    else
                    {
                        Layer.Mask = gradient;
                    }
                };
            }
      
        }
    }
}

Upvotes: 2

Related Questions