【问题标题】:How to create a progress bar with rounded corners in iOS using Xamarin.Forms如何使用 Xamarin.Forms 在 iOS 中创建带圆角的进度条
【发布时间】:2018-07-14 22:42:40
【问题描述】:

我使用来自此SO Question 的输入来使用DrawableAndroid 平台创建一个带有圆角的自定义进度条。 但是我无法为iOS 创建相同的输出。

下面是它在Android 上的样子。

如何在iOS 中也创建相同的效果?

【问题讨论】:

    标签: c# ios xamarin xamarin.forms xamarin.ios


    【解决方案1】:

    有很多方法可以做到这一点,我更喜欢使用CALayerUIView 并添加一个绘制“进度条”的CAShapeLayer

    UIView 进度条示例:

    public class ProgressView : UIView
    {
        CAShapeLayer progressLayer;
        UILabel label;
    
        public ProgressView() { Setup(); }
        public ProgressView(Foundation.NSCoder coder) : base(coder) { Setup(); }
        public ProgressView(Foundation.NSObjectFlag t) : base(t) { Setup(); }
        public ProgressView(IntPtr handle) : base(handle) { }
        public ProgressView(CGRect frame) : base(frame) { Setup(); }
    
        void Setup()
        {
            BackgroundColor = UIColor.Clear;
    
            Layer.CornerRadius = 25;
            Layer.BorderWidth = 10;
            Layer.BorderColor = UIColor.Blue.CGColor;
            Layer.BackgroundColor = UIColor.Cyan.CGColor;
    
            progressLayer = new CAShapeLayer()
            {
                FillColor = UIColor.Red.CGColor,
                Frame = Bounds
            };
            Layer.AddSublayer(progressLayer);
    
            label = new UILabel(Bounds)
            {
                TextAlignment = UITextAlignment.Center,
                TextColor = UIColor.White,
                BackgroundColor = UIColor.Clear,
                Font = UIFont.FromName("ChalkboardSE-Bold", 20)
            };
            InsertSubview(label, 100);
        }
    
        double complete;
        public double Complete
        {
            get { return complete; }
            set { complete = value; label.Text = $"{value * 100} %"; SetNeedsDisplay(); }
        }
    
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
            var progressWidth = (rect.Width - (Layer.BorderWidth * 2)) * complete;
            var progressRect = new CGRect(rect.X + Layer.BorderWidth, rect.Y + Layer.BorderWidth, progressWidth, (rect.Height - Layer.BorderWidth * 2));
            progressLayer.Path = UIBezierPath.FromRoundedRect(progressRect, 25).CGPath;
        }
    }
    

    使用示例:

    var progressView = new ProgressView(new CGRect(50, 50, 300, 50));
    Add(progressView);
    DispatchQueue.MainQueue.DispatchAsync(async () =>
    {
        while (progressView.Complete <= 1)
        {
            InvokeOnMainThread(() => progressView.Complete += 0.05);
            await Task.Delay(1000);
        }
    });
    

    【讨论】:

    • 感谢您的详细回答。您还可以帮助我如何使用CustomRendererXamarin.Forms 来实现此方法吗?。
    • @LibinTK Xamarin 有一个指南,展示了如何为表单实现自定义视图渲染器:developer.xamarin.com/guides/xamarin-forms/…
    • 如何在android中显示相同的UI?
    • @Singapore 用您目前拥有的代码和您的问题提出一个新问题。
    • @SushiHangover感谢您的回答。我已经使用您的答案制作了一个从右侧开始的进度条。 var progressWidth = (rect.Width - (Layer.BorderWidth * 2)) * (1 - complete); var progressRect = new CGRect(rect.X + progressWidth, rect.Y + Layer.BorderWidth, rect.Width - progressWidth, (rect.Height - Layer.BorderWidth * 2)); progressLayer.Path = UIBezierPath.FromRoundedRect(progressRect, (UIRectCorner.TopRight | UIRectCorner.BottomRight), new CGSize(30, 30)).CGPath; 但我没有看到标签。
    猜你喜欢
    • 2019-02-14
    • 2017-07-27
    • 2016-08-06
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    相关资源
    最近更新 更多