圆形旋转五个按钮保持相等的距离

时间:2011-03-25 14:15:05

标签: iphone objective-c ipad ipod

我想在圆周上旋转五个按钮(圆心与中心(120,120),实际上是方形视图的中心,即240 * 240),半径为100.是否可以这样做,有交互使用旋转和正确外观的按钮。

我试过'

x =  round(cx + redious * cos(angl));
y =  round(cy - redious * sin(angl));   

NSString *X=[NSString stringWithFormat:@"%f",x];
[xval addObject:[NSString stringWithFormat:@"%@",X]];
NSString *Y=[NSString stringWithFormat:@"%f",y];
[yval addObject:[NSString stringWithFormat:@"%@",Y]];'

计算点数:

map.frame= CGRectMake([[fqx objectAtIndex:counter1]intValue],[[fqy objectAtIndex:counter1]intValue], 72, 37);   
website.frame= CGRectMake([[fqx objectAtIndex:counter2]intValue],[[fqy objectAtIndex:counter2]intValue], 72, 37);   
share.frame= CGRectMake([[fqx objectAtIndex:counter3]intValue],[[fqy objectAtIndex:counter3]intValue], 72, 37); 
slideShow.frame= CGRectMake([[fqx objectAtIndex:counter4]intValue],[[fqy objectAtIndex:counter4]intValue], 72, 37);' 

旋转,但它产生奇怪的路径..以三角形方式.. ('map','share','slideShow','website')ar我的按钮..:P

1 个答案:

答案 0 :(得分:4)

这太可爱了,不能放弃,所以在这里。此代码仅限于一个按钮旋转,但我怀疑你将它扩展为多个(提示:使用一个CADisplayLink并同时旋转所有按钮)。

- (void)setupRotatingButtons
{
    // call this method once; make sure "self.view" is not nil or the button 
    // won't appear. the below variables are needed in the @interface.
    // center: the center of rotation
    // radius: the radius
    // time:   a CGFloat that determines where in the cycle the button is located at
    //         (note: it will keep increasing indefinitely; you need to use 
    //         modulus to find a meaningful value for the current position, if
    //         needed)
    // speed:  the speed of the rotation, where 2 * M_PI is 1 lap a second
    // b:      the UIButton
    center = CGPointMake(100, 100);
    radius = 100;
    time = 0;
    speed = 2 * M_PI; // <-- will rotate CW 360 degrees per second (1 "lap"/s)

    b = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    b.titleLabel.text = @"Hi";
    b.frame = CGRectMake(0.f, 0.f, 100, 50);
    // we get the center set right before we add subview, to avoid glitch at start
    [self continueCircling:nil]; 
    [self.view addSubview:b];
    [self.view bringSubviewToFront:b];
    CADisplayLink *dl = [CADisplayLink displayLinkWithTarget:self 
        selector:@selector(continueCircling:)];
    [dl addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

我们还需要实际的“continueCircling:”方法,它只是:

- (void)continueCircling:(CADisplayLink *)dl
{
    time += speed * dl.duration;
    b.center = CGPointMake(center.x + radius * cosf(time), 
                           center.y + radius * sinf(time));
}

我确信还有其他更好的方法可以解决这个问题,但至少可以解决上述问题。 :)

编辑:我忘了提及,你需要添加QuartzCore框架和

#import <QuartzCore/QuartzCore.h> 

用于CADisplayLink。

编辑2:找到PI常量(M_PI),因此将其替换为3.1415。