gpt4 book ai didi

ios - 计算 subview 在 UIView 圆上的圆心坐标(倒坐标系)

转载 作者:行者123 更新时间:2023-11-29 02:58:16 25 4
gpt4 key购买 nike

我想将一个 UIButton/UIView(红点)的中心定位在一个假想圆的边界上,作为另一个 UIView 的 subview ——这意味着坐标系是倒置的。

enter image description here

如何计算圆上红色框中心的 CGPoint。这是我所拥有的……

  • (CGPoint) 圆心 x,y
  • ( float )圆半径 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

我实际上希望绿色按钮位于粉红色圆圈的顶部。所以还没有到那一步。

值(value)观:

  • 弧度 10.995574
  • 弧度 12.042772

最佳答案

导入<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);

如果您的角度以度为单位,则需要将其转换为弧度。

关于ios - 计算 subview 在 UIView 圆上的圆心坐标(倒坐标系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23620515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com