gpt4 book ai didi

ios - 在IOS上自定义渲染Frame的CardView

转载 作者:行者123 更新时间:2023-12-01 18:06:54 25 4
gpt4 key购买 nike

我试图通过子类化Frame并制作一个自定义渲染器来在IOS Xamarin Forms上制作CardView。我想实现以下目标:

enter image description here

在研究设置阴影的API时,我已完成以下操作:

[assembly: ExportRenderer(typeof(CardView), typeof(CardViewRenderer))]
namespace TrabbleMobile.iOS.CustomRenderers
{
public class CardViewRenderer : FrameRenderer
{
public override void Draw(CGRect rect)
{
var cardView = (CardView)this.Element;

using (var context = UIGraphics.GetCurrentContext())
{
//nfloat cornerRadius = 2;
float shadowOffsetWidth = 2;
float shadowOffsetHeight = 4;
var shadowColor = new CGColor(0, 0, 0, (nfloat)0.5);
var boxColor = new CGColor(255, 255, 255);
var shadowBlur = (float)0.5;
var offset = new CGSize(shadowOffsetWidth, shadowOffsetHeight);

context.SetShadow(offset, shadowBlur, shadowColor);

但是,它不会呈现应有的渲染,也完全没有阴影。

最佳答案

我已经做到了,而这样做的方式是通过在IOS上进行自定义渲染,这是自定义渲染器代码:

    public class CardViewRenderer : FrameRenderer
{

public override void Draw(CGRect rect)
{
SetupShadowLayer();
base.Draw(rect);
}

void SetupShadowLayer()
{
Layer.CornerRadius = 2; // 5 Default
if (Element.BackgroundColor == Xamarin.Forms.Color.Default)
{
Layer.BackgroundColor = UIColor.White.CGColor;
}
else
{
Layer.BackgroundColor = Element.BackgroundColor.ToCGColor();
}

Layer.ShadowRadius = 2; // 5 Default
Layer.ShadowColor = UIColor.Black.CGColor;
Layer.ShadowOpacity = 0.4f; // 0.8f Default
Layer.ShadowOffset = new CGSize(0f, 2.5f);

if (Element.OutlineColor == Xamarin.Forms.Color.Default)
{
Layer.BorderColor = UIColor.Clear.CGColor;
}
else
{
Layer.BorderColor = Element.OutlineColor.ToCGColor();
Layer.BorderWidth = 1;
}

Layer.RasterizationScale = UIScreen.MainScreen.Scale;
Layer.ShouldRasterize = true;
}
}

关于ios - 在IOS上自定义渲染Frame的CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com