在iOS.Xamarin(monotouch)中将球拖过圆圈

时间:2018-05-03 10:54:22

标签: xamarin xamarin.ios

我是Xamarin iOS平台的初学者。我想把拖球放在圆圈边界上。请在下面找到更多详细信息。在此先感谢:) enter image description here

1 个答案:

答案 0 :(得分:0)

我只是将Swift转换为C#,原始代码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