在圆上放置按钮

Jus*_*ney 1 iphone objective-c ipad ios

我在Objective-C中计算圆圈上的点数时间很长.即使在阅读了其他代码示例的TONS后,我的圈子仍然偏离中心.(这是考虑到"中心"与"原点"并调整UIView的大小,在这种情况下是UIButton.)

这是我正在使用的代码.圆圈形成正确,它正好偏离中心.我不确定这是一个弧度与度数问题还是别的.这是ViewController中的一个辅助函数,它以编程方式创建UIButtons并将它们添加到视图中:

- (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;   
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

How*_*ard 5

按钮的中心(即圆圈上的点)是

newPoint.x = (centerPoint.x) + (radius * cosf(angle));  // <= removed / 2
newPoint.y = (centerPoint.y) + (radius * sinf(angle));  // <= removed / 2
Run Code Online (Sandbox Code Playgroud)

请注意,如果你把这些点,你必须确保按钮(即矩形),他们的中心位于该点(即减去buttonWidth/2newPoint.xbuttonHeight/2newPoint.y获得左上角).