首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >制作16个uilabels,并将它们按圆形路径对齐。

制作16个uilabels,并将它们按圆形路径对齐。
EN

Stack Overflow用户
提问于 2013-08-01 15:43:54
回答 2查看 233关注 0票数 1
代码语言:javascript
复制
NSMutableArray *views = [[NSMutableArray alloc]initWithCapacity:0];

    for (NSInteger i = 0; i<16; i++)
    {
        UIView *circle = [[UIView alloc]init];
        circle.backgroundColor = [UIColor clearColor];
        UIImageView *circleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
        circleImage.image = [UIImage imageNamed:@"circle"];
        [circle addSubview:circleImage];
        UILabel *labelInsideCircle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        labelInsideCircle.backgroundColor = [UIColor clearColor];
        labelInsideCircle.textColor = [UIColor greenColor];
        labelInsideCircle.font = [UIFont fontWithName:@"Helvetica" size:30.0];
        labelInsideCircle.center = circleImage.center;
        NSInteger int_ = [self getRandomNumber:0 to:(arrOfOptions.count-1)];
        labelInsideCircle.text = [NSString stringWithFormat:@"%@",[arrOfOptions objectAtIndex:int_]];
        labelInsideCircle.textAlignment = NSTextAlignmentCenter;
        [arrOfOptions removeObjectAtIndex:int_];
        [circle addSubview:labelInsideCircle];
        [labelInsideCircle release];
        [views addObject:circle];
        [circle release];
        [circleImage release];
    }


    /* Rotating circles with angles  */

    float curAngle = 0;
    float incAngle = ( 360.0/(views.count) )*3.14/180.0;
    CGPoint circleCenter = CGPointMake(380, 580); /* given center */
    float circleRadius = 250; /* given radius */
    for (UIView *view in views)
    {
        CGPoint viewCenter;
        viewCenter.x = circleCenter.x + cos(curAngle)*circleRadius;
        viewCenter.y = circleCenter.y + sin(curAngle)*circleRadius;
        view.transform = CGAffineTransformRotate(view.transform, curAngle);
        view.center = viewCenter;
        [self.view addSubview:view];

        curAngle += incAngle;
    }

这里的问题是UILabel的文本也被转换了,这是显而易见的。我想要的是16个圆形视图上的标签,没有标签的文本转换。有人能帮我解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2013-08-01 16:06:07

在这种情况下,您只需要更改它们的位置坐标,而不是旋转它们。

代码语言:javascript
复制
NSMutableArray *views = [[NSMutableArray alloc] initWithCapacity:0];

for (NSInteger i = 0; i<16; i++)
{
    UIView *circle = [[UIView alloc]init];
    circle.backgroundColor = [UIColor clearColor];
    UIImageView *circleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    circleImage.image = [UIImage imageNamed:@"circle"];
    [circle addSubview:circleImage];
    UILabel *labelInsideCircle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    labelInsideCircle.backgroundColor = [UIColor clearColor];
    labelInsideCircle.textColor = [UIColor greenColor];
    labelInsideCircle.font = [UIFont fontWithName:@"Helvetica" size:30.0];
    labelInsideCircle.center = circleImage.center;
    NSInteger int_ = arc4random()%[arrOfOptions count];
    labelInsideCircle.text = [NSString stringWithFormat:@"%@",[arrOfOptions objectAtIndex:int_]];
    labelInsideCircle.textAlignment = NSTextAlignmentCenter;
    [arrOfOptions removeObjectAtIndex:int_];
    [circle addSubview:labelInsideCircle];
    [labelInsideCircle release];
    [views addObject:circle];
    [self.view addSubview:circle];
    [circle release];
    [circleImage release];
}


/* Rotating circles with angles  */

float curAngle = 0;
float incAngle = ( 360.0/(views.count) )*3.14/180.0;
CGPoint circleCenter = CGPointMake(380, 580); /* given center */
float circleRadius = 250; /* given radius */
for (UIView *view in views)
{
    CGPoint viewCenter;
    viewCenter.x = circleCenter.x + cos(curAngle)*circleRadius;
    viewCenter.y = circleCenter.y + sin(curAngle)*circleRadius;
    //view.transform = CGAffineTransformRotate(view.transform, curAngle);
    view.center = viewCenter;
    [self.view addSubview:view];

    curAngle += incAngle;
}

一些编码建议:如果您自己的随机数生成器中没有任何特殊功能,则可以使用arc4random()函数。你可以在xCode中打开圆弧!

票数 0
EN

Stack Overflow用户

发布于 2013-08-01 18:32:35

在循环中,相应地设置它们的中心,不要旋转它们,也不要旋转它们的超级视图。

以下内容与您的问题没有直接关系,但可能会有所帮助:

显然,你已经掌握了所有可用的数学知识。我建议测量在cos和sin等调用上损失了多少cpu时间。如果你觉得这很重要,那就考虑一下你的算法。你会(很有可能)发现你对有限数量的角度调用了cos和sin几百次甚至上千次。然后,您可以尝试它,并发现可能的预计算或只是“缓存和重用”较早的结果可能会节省大量的处理时间。

加上预先计算或缓存的窦就行了。您可以通过向参数添加(或分别减去) pi/2 (或分别为90度)的偏移量,从正弦派生cosinus值(反之亦然)。

对于类似的任务,我使用度数(而不是辐射度),发现我无法预测角度,但显著的满度(1°,2°,3°,...以及介于两者之间的任何东西)对这项工作来说还不够精确。然后,我维护了一个包含360个正弦值的数组,并使用它来代替一次又一次地调用真正的函数。(另外,我只需要计算其中的90个,并根据窦功能的性质镜像其他270度的结果) 180个浮点与我获得的速度相比并不是太多的内存。像这样的东西也可以适合你。在您的例子中,您对sin和cos的潜在兴趣仅限于incAngle的全数多编译器。

这意味着每个潜在值只有[views count]个数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17987968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档