0

I am beginner in Xamarin iOS platform. I want the drag ball over the circle border. Please find below image for more details. Thanks in advance :) enter image description here

1 Answers1

0

I just convert the Swift to C# , origin code here.

nfloat midViewX;
nfloat midViewY;

UIBezierPath circlePath2;
CAShapeLayer shapeLayer2 = new CAShapeLayer();

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    midViewX = UIScreen.MainScreen.Bounds.Width / 2;
    midViewY = UIScreen.MainScreen.Bounds.Height / 2;

    var circlePath = UIBezierPath.FromArc(new CGPoint(midViewX, midViewY), 100, 0, (nfloat)(Math.PI*2), true);
    var shapeLayer = new CAShapeLayer();
    shapeLayer.Path = circlePath.CGPath;
    shapeLayer.FillColor = UIColor.Clear.CGColor;
    shapeLayer.StrokeColor = UIColor.Red.CGColor;
    shapeLayer.LineWidth = 3;
    View.Layer.AddSublayer(shapeLayer);

    var angleEarthAfterCalculate = (nfloat)(180 * Math.PI / 180 - Math.PI / 2);
    var earthX = midViewX + Math.Cos(angleEarthAfterCalculate) * 100;
    var earthY = midViewY + Math.Sin(angleEarthAfterCalculate) * 100;

    circlePath2 = UIBezierPath.FromArc(new CGPoint(earthX, earthY), 10, 0, (nfloat)(Math.PI * 2), true);

    shapeLayer2.Path = circlePath2.CGPath;
    shapeLayer2.FillColor = UIColor.Blue.CGColor;
    shapeLayer2.StrokeColor = UIColor.Clear.CGColor;
    shapeLayer2.LineWidth = 7;
    View.Layer.AddSublayer(shapeLayer2);

    var dragBall = new UIPanGestureRecognizer(DragBall);
    View.AddGestureRecognizer(dragBall);
}

void DragBall(UIPanGestureRecognizer recognizer)
{
    var point = recognizer.LocationInView(View);
    var earthX = point.X;
    var earthY = point.Y;
    var midViewXDouble = midViewX;
    var midViewYDouble = midViewY;
    var angleX = earthX - midViewXDouble;
    var angleY = earthY - midViewYDouble;
    var angle = Math.Atan2(angleY, angleX);
    var earthX2 = midViewXDouble + Math.Cos(angle) * 100;
    var earthY2 = midViewYDouble + Math.Sin(angle) * 100;

    circlePath2 = UIBezierPath.FromArc(new CGPoint(earthX2, earthY2), 10, 0, (nfloat)(Math.PI * 2), true);
    shapeLayer2.Path = circlePath2.CGPath;
}

enter image description here

ColeX
  • 14,062
  • 5
  • 43
  • 240