在UIView(倒置坐标系)中的圆上计算Subview的中心坐标

时间:2014-05-12 23:24:28

标签: ios objective-c math uiview geometry

我想将UIButton / UIView(红点)的中心定位在假想圆的边界上作为另一个UIView的Subview - 这意味着坐标系被反转。

enter image description here

如何计算圆上红框中心的CGPoint。这就是我的......

  • (CGPoint)circleCenter x,y
  • (float)circleRadius r
  • (float)angleAsRadian⍺

这是我正在尝试的(感谢@rmaddy)

CGFloat buttonX = circleCenter.x + cos(angleAsRadian) * radius;
CGFloat buttonY = circleCenter.y - sin(angleAsRadian) * radius;

CGPoint buttonCenter = CGPointMake(buttonX, buttonY);

UIButton* buttonMarker = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 20, 20)];
[buttonMarker addTarget:self action:@selector(showTitle:) forControlEvents:UIControlEventTouchUpInside];
[buttonMarker setBackgroundColor:[UIColor greenColor]];
[buttonMarker setCenter:buttonCenter];
[_view addSubview:buttonMarker];

...和当前结果:

enter image description here

我实际上期待绿色按钮位于粉色圆圈的顶部。所以还没到那里。

值:

  • Radian 10.995574
  • Radian 12.042772

1 个答案:

答案 0 :(得分:1)

导入<math.h>并执行:

CGPoint circle = ... // the center of the circle
CGFloat angle = ... // the angle in radians
CGFloat radius = ...// the radius

CGFloat buttonX = circle.x + cos(angle) * radius;
CGFloat buttonY = circle.y - sin(angle) * radius;

CGPoint buttonCenter = CGPointMake(buttonX, buttonY);

如果您有角度度,则需要将其转换为弧度。