iphone - 如何将图像动态排列在一个圆圈中

标签 iphone ios xcode geometry

我如何在圆形图像中动态排列图标(UIButtons,带有背景图像)。类似的东西应该像同心圆。

我正在使用以下代码,

UIImageView *container = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0f, 299.0f, 298.0f)];
    container.image = [UIImage imageNamed:@"AR_RF004_circle.png"];
    container.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
    [self.view addSubview:container];

    NSArray *imagesArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"AR1.png"],[UIImage imageNamed:@"AR2.png"],[UIImage imageNamed:@"AR3.png"],[UIImage imageNamed:@"AR4.png"],[UIImage imageNamed:@"AR5.png"], nil];
int numberOfSections = 5;
    CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j < numberOfSections; ++j) {
        UIButton *sectionLabel = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 128.0f)];
[sectionLabel setBackgroundImage:[imagesArray objectAtIndex:j] forState:UIControlStateNormal];
sectionLabel.layer.anchorPoint = CGPointMake(0.9f, 0.1f);
        sectionLabel.layer.position = CGPointMake(container.bounds.size.width/2.0, container.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.

        sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*j);
        [container addSubview:sectionLabel];
    NSLog(@"section label x and y is %f ,%f",sectionLabel.frame.origin.x,sectionLabel.frame.origin.y);
    }
    [container release];

提前致谢

最佳答案

您可以使用这段代码:

UIButton *currentButton;
int buttonCount = 20;
int radius = 200; 
float angleBetweenButtons = (2 * M_PI) / buttonCount;
float x = 0;
float y = 0;
CGAffineTransform rotationTransform;

for (int i = 0; i < buttonCount; i++)
{
    // get your button from array or something
    currentButton = ...
    ...

    x = radius + cosf(i * angleBetweenButtons) * radius;
    y = radius + sinf(i * angleBetweenButtons) * radius;
    rotationTransform = CGAffineTransformIdentity;
    rotationTransform = CGAffineTransformRotate(rotationTransform, (i * angleBetweenButtons));
    currentButton.center = CGPointMake(x, y);
    currentButton.transform = rotationTransform;
}

关于iphone - 如何将图像动态排列在一个圆圈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12562458/

相关文章:

ios - 当应用程序处于纵向模式时旋转 MPMovies PlayerViewController

ios - iOS 应用外观是否取决于部署目标?

ios - 如何在没有互联网的情况下在 iPhone 上调试应用程序

iphone - iOS Core Audio 渲染回调适用于模拟器,不适用于设备

iphone - 从应用程序访问 iPhone 联系人

Objective-C:来自 NSTask 的多个 View 和数据

iphone - 检测 2 次滑动而不中断触摸

iOS - 确定坐标是否在 GMSPolyline 内

swift - (swift/xcode) 错误消息 : 'Setting this node as parent would create a loop'

iphone - 在 Mpmovieplayer 或 Mpmusicplayer ios 中播放 youtube 视频