将按钮放置在圆上
我在 Objective-C 中计算圆上的点很费时间。即使在阅读了大量其他代码示例之后,我的圈子仍然偏离中心。 (这要考虑“中心”与“原点”,并调整 UIView 的大小,在本例中为 UIButton。)
这是我正在使用的代码。圆圈形成正确,只是偏离中心。我不确定这是弧度与度数的问题还是其他问题。这是 ViewController 中的一个辅助函数,它以编程方式创建 UIButton 并将它们添加到视图中:
- (CGPoint)pointOnCircle:(int)thisPoint withTotalPointCount:(int)totalPoints {
CGPoint centerPoint = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
float radius = 100.0;
float angle = ( 2 * M_PI / (float)totalPoints ) * (float)thisPoint;
CGPoint newPoint;
newPoint.x = (centerPoint.x / 2) + (radius * cosf(angle));
newPoint.y = (centerPoint.y / 2) + (radius * sinf(angle));
return newPoint;
}
感谢您的帮助!
I'm having a heck of a time calculating points on a circle in Objective-C. Even after reading TONS of other code samples my circle is still way off center. (And that's taking into consideration "center" vs "origin" and adjusting for the size of the UIView, in this case a UIButton.)
Here's the code I'm using. The circle is formed correctly, it's just off center. I'm not sure if this is a radians vs degrees problem or something else. This is a helper function in a ViewController that programmatically creates the UIButtons and adds them to the view:
- (CGPoint)pointOnCircle:(int)thisPoint withTotalPointCount:(int)totalPoints {
CGPoint centerPoint = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
float radius = 100.0;
float angle = ( 2 * M_PI / (float)totalPoints ) * (float)thisPoint;
CGPoint newPoint;
newPoint.x = (centerPoint.x / 2) + (radius * cosf(angle));
newPoint.y = (centerPoint.y / 2) + (radius * sinf(angle));
return newPoint;
}
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按钮的中心(即圆上的点)是
请注意,如果您将按钮(即矩形)放在这些点上,则必须确保它们的中心位于该点(即减去
buttonWidth/2newPoint.x
的 code> 和来自newPoint.y
的buttonHeight/2
来获取左上角)。The center of your buttons (i.e. points on the circle) is
Please note that if you place buttons (i.e. rectangles) on these points you have to make sure that their center lies at this point (i.e. subtract
buttonWidth/2
fromnewPoint.x
andbuttonHeight/2
fromnewPoint.y
to get the top left corner).不知道为什么你第二次除以 2 newPoint 坐标...
这个怎么样:
Not sure why you divide by 2 a second time newPoint coordinates...
What about this: