c# - 在 Xamarin.iOS (Xamarin Monotouch) 中绘制圆圈以图形方式显示进度

标签 c# ios xamarin xamarin.ios

因为我对 Xamarin World 及其控件还很陌生。我想在我的单触摸应用程序中添加圆圈以显示工作进度。为了显示进度,我必须在圆圈中标记一条弧线。如果可能的话,任何人都可以帮助我提供示例代码。等待答案,在此先感谢。 i like to add image like this with two text filed init

最佳答案

Result of the view

using System;
    using UIKit;
    using CoreGraphics;

    namespace CircleTest.Touch
    {
        public class CircleGraph : UIView
        {
            int _radius = 10;
            int _lineWidth = 10;
            nfloat _degrees = 0.0f;
            UIColor _backColor = UIColor.FromRGB(46, 60, 76);
            UIColor _frontColor = UIColor.FromRGB(234, 105, 92);
            //FromRGB (234, 105, 92);

        public CircleGraph (CGRect frame, int lineWidth, nfloat degrees)
        {
            _lineWidth = lineWidth; 
            _degrees = degrees; 
            this.Frame = new CGRect(frame.X, frame.Y, frame.Width, frame.Height);
            this.BackgroundColor = UIColor.Clear; 

        }

        public CircleGraph (CGRect frame, int lineWidth, UIColor backColor, UIColor frontColor)
        {
            _lineWidth = lineWidth;
            this.Frame = new CGRect(frame.X, frame.Y, frame.Width, frame.Height);
            this.BackgroundColor = UIColor.Clear;

        }

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

            using (CGContext g = UIGraphics.GetCurrentContext ()) {
                _radius = (int)( (this.Bounds.Width) / 3) - 8;
                DrawGraph(g, this.Bounds.GetMidX(), this.Bounds.GetMidY());
            };
        }

        public void DrawGraph(CGContext g,nfloat x0,nfloat y0) {
            g.SetLineWidth (_lineWidth);

            // Draw background circle
            CGPath path = new CGPath ();
            _backColor.SetStroke ();
            path.AddArc (x0, y0, _radius, 0, 2.0f * (float)Math.PI, true);
            g.AddPath (path);
            g.DrawPath (CGPathDrawingMode.Stroke);

            // Draw overlay circle
            var pathStatus = new CGPath ();
            _frontColor.SetStroke ();
            pathStatus.AddArc(x0, y0, _radius, 0, _degrees * (float)Math.PI, false);
            g.AddPath (pathStatus);
            g.DrawPath (CGPathDrawingMode.Stroke);
        }
    }
}

实际上这就是我真正应该做的。它对我有用

这是它的样子。你可以像类文件一样创建它,你可以简单地分配给 UIView。 如需更多引用,您可以使用此示例项目 Pi Graph

[编辑]: Draw 方法最初将 View.Frame x,y 传递给 DrawGraph 方法。这应该是 View.Bounds(在上面进行了修改以反射(reflect)这一点)。请记住,框架 x,y 引用包含的超 View ,边界引用当前 View 。如果在 0,0 处添加 View ,这会起作用,但是一旦您开始在 UI 中移动,它就会消失。绘制圆弧时,传递给 AddArc 的 x、y 值需要引用当前 View 而不是父父 View 。

关于c# - 在 Xamarin.iOS (Xamarin Monotouch) 中绘制圆圈以图形方式显示进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693428/

相关文章:

android - 是否可以在一些人使用 Xamarin IDE 而其他人使用 Visual Studio 的团队中工作?

c# - Dataset.ReadXml() - 给定编码中的无效字符

ios - 多次出现权限弹出窗口

iOS Swift - 如何匹配另一个字符串为 "like"的字符串?即 "http"就像 "http://"

c# - Xamarin 随机不识别名字

android - 在 xamarin 中使用交错网格

c# - 从 Session 中清理非托管资源,如 Crystal Report Document

c# - NHibernate 映射属性 + 脏检查

c# - 无法在 Visual Studio (Mac) 上使用 .NET Core 3.1 创建 Web 应用

iphone - UIDevice systemVersion 有时包含操作系统名称