Correctly draw circle on given x, y

Problem as stated above, it's drawing approx. 100x more to the right and 70 y below the given x, y. Can someone help?

            using (var g = UIGraphics.GetCurrentContext())
            {
                g.SetLineWidth(1f);
                g.SetFillColor(Fill);
                g.SetStrokeColor(Stroke);

                CGPath path = new CGPath();
                path.AddArc(x: rect.X + (rect.Width / 2),
                            y: rect.Y + (rect.Height / 2),
                            radius: NMath.Min(rect.Width, rect.Height) / 2  - 1f,
                            startAngle: 0f,
                            endAngle: 2.0f * NMath.PI,
                            clockwise: true);
                g.AddPath(path);
                g.DrawPath(CGPathDrawingMode.FillStroke);
            }

Any changes result in the circle not drawing or partially. Thanks in advance.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited December 2018

    I think you just need to modify the X and Y position on its superView(the yellow view).

    My code

    namespace iOSApp
    {
        public class myView : UIView
        {
            public override void Draw(CGRect rect)
            {
                base.Draw(rect);
    
                using (var g = UIGraphics.GetCurrentContext())
                {
                    g.SetLineWidth(1f);
                    g.SetFillColor(UIColor.Red.CGColor);
                    g.SetStrokeColor(UIColor.Green.CGColor);
    
                    CGPath path = new CGPath();
                    path.AddArc(x: rect.X + (rect.Width / 2),
                                y: rect.Y + (rect.Height / 2),
                                radius: NMath.Min(rect.Width, rect.Height) / 2 - 1f,
                                startAngle: 0f,
                                endAngle: 2.0f * NMath.PI,
                                clockwise: true);
                    g.AddPath(path);
                    g.DrawPath(CGPathDrawingMode.FillStroke);
                }
            }
        }
    
        public partial class VC : UIViewController
        {
            public VC (IntPtr handle) : base (handle)
            {
            }      
    
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
    
    
                myView view = new myView();
                view.BackgroundColor = UIColor.Yellow;
                view.Frame = new CGRect(100, 100, 100, 100);
                View.Add(view);
    
                UIButton button = new UIButton(new CGRect(200, 200, 100, 50));
                button.SetTitle("Change",UIControlState.Normal);
                button.SetTitleColor(UIColor.Black, UIControlState.Normal);
                button.TouchUpInside += (sender,e) =>
                {
                    CGRect rect = view.Frame;
                    rect.X -= 100;
                    rect.Y -= 70;
                    view.Frame = rect;
                };
                View.Add(button);
            }
        }
    }
    

Sign In or Register to comment.