1
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個の円形ビューです。誰でもこれで私を助けてもらえますか?

4

2 に答える 2

0

ループでは、それに応じて中心を設定します。回転させたり、スーパー ビューを回転させたりしないでください。

以下は質問とは直接関係ありませんが、参考になるかもしれません。

明らかに、すべての数学が利用可能です。cos や sin などの呼び出しで失われる CPU 時間を測定することをお勧めします。それが重要であることがわかった場合は、アルゴリズムについて考えてください。(ほとんどの場合) cos と sin を数百回または数千回、限られた角度で呼び出すことがわかります。次に、それを試してみて、可能な事前計算または以前の結果を「キャッシュして再利用」するだけで、処理時間を大幅に節約できることがわかります。

さらに、副鼻腔の事前計算またはキャッシュが必要です。引数に pi/2 (またはそれぞれ 90 度) のオフセットを追加 (またはそれぞれ減算) することにより、正弦から余弦値 (およびその逆) を導き出すことができます。

同様のタスクで、度数 (ラジアンではない) で作業していたところ、角度を予測できなかったが、十分な度数 (1°、2°、3°、... およびその間に何もない) が十分に正確であることがわかりました。仕事で。次に、実際の関数を何度も呼び出す代わりに、360 個の正弦値の配列を維持し、それを使用しました。(さらに、正弦関数の性質に応じて、そのうちの 90 個を計算し、他の 270 度の結果をミラーリングするだけで済みました) 180 個のフロートは、得られた速度に比べてメモリが多すぎません。そのようなものもあなたに適している可能性があります。あなたの場合、sin と cos に対する潜在的な引数は、incAngle のフル数の乗数に制限されています。

これは、[views count]それぞれの潜在的な値の数だけがあることを意味します。

于 2013-08-01T10:32:34.610 に答える
0

この場合、位置座標を変更するだけで、回転する必要はありません。

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 で ARC を有効にできます。

于 2013-08-01T08:06:06.850 に答える