如何在Xamarin Forms iOS中淡出滚动视图的内容?
我在android上构建了一个自定义的scrollview渲染器,可以淡出底部的文本 如何对iOS执行相同的操作?我想不出来。 我知道如何实现渲染器,但不知道如何使其褪色 我在我的主要xamarin项目中添加了一个空的FadeScrollView类如何在Xamarin Forms iOS中淡出滚动视图的内容?,ios,forms,xamarin,xamarin.forms,uiscrollview,Ios,Forms,Xamarin,Xamarin.forms,Uiscrollview,我在android上构建了一个自定义的scrollview渲染器,可以淡出底部的文本 如何对iOS执行相同的操作?我想不出来。 我知道如何实现渲染器,但不知道如何使其褪色 我在我的主要xamarin项目中添加了一个空的FadeScrollView类 public class FadeScrollView : ScrollView { } 然后在我的xaml页面中加载这个scrollview渲染器 <ContentPage ... xmlns:customRen
public class FadeScrollView : ScrollView
{
}
然后在我的xaml页面中加载这个scrollview渲染器
<ContentPage ...
xmlns:customRenders="clr-namespace:MyNameSpace;assembly=MyNameSpace"
>
...
<customRenderers:FadeScrollView>
....
</FadeScrollView>
</ContentPage>
这将产生一个预期的结果:
现在我以类似的方式设置iOS渲染器,但我不确定如何实现淡入淡出效果,当我在谷歌上搜索时,我只发现有人向你展示如何使用swift
[assembly: ExportRenderer(typeof(FadeScrollView), typeof(FadeScrollViewRendererIOS))]
namespace MyNameSpace
{
class FadeScrollViewRendererIOS : UIScrollView
{
public FadeScrollViewRendererIOS()
{
//fade out
}
}
}
iOS似乎没有在Android中提供类似于垂直FadingEnabled的api 我们可以在带有渐变层的自定义渲染器中实现这一点 请尝试下面的代码
[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;
}
};
}
}
}
}
只需在ScrollView的顶部添加一个渐变视图就行了,但我只想在列表实际可滚动时显示截止效果。如果我添加固定的透明度,适合滚动视图的文本也会被切掉。
[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;
}
};
}
}
}
}