2011-03-25 38 views
1

我想在一个圆的圆周上旋转五个按钮(圆心(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);' 

旋转,但它产生怪异的路径。在三角形的方式.. (' 地图”, '分享' ,'slideShow','website')我的按钮..:P

+1

你的“angl”辐射?它一定要是。 radAngl = angl/180 * Pi。 另外:不要基于以前的坐标的下一个迭代。只增加角度并使用初始位置计算位置。 – Krumelur 2011-03-25 14:19:34

+0

不,这是我正在递减的角度 – rptwsthi 2011-03-25 14:25:11

+0

@vladimir:你能告诉我你是怎么做到的吗? :) – rptwsthi 2011-03-25 14:26:07

回答

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代替。

+0

你是我的英雄..谢谢SOOOO ..很.. ..我将进一步努力.. – rptwsthi 2011-03-26 05:48:40

+0

是的,这听起来像它应该工作。如果你想要等距离,你希望它们之间的每个“步长”都是2 * PI的除数。我确定PI是一个常量,但是使用3.1415可能足够接近。 – Kalle 2011-03-26 09:46:03

+0

嘿我为剩下的4做了什么:'c.center = CGPointMake(center.x + radius * cosf(time + 1.3),center.y + radius * sinf(time + 1.3));'' d.center = CGPointMake(center.x + radius * cosf(time + 2.6),center.y + radius * sinf(time + 2.6)); e.center = CGPointMake(center.x + radius * cosf(time + 3.9),center.y + radius * sinf(time + 3.9)); f.center = CGPointMake(center.x + radius * cosf(time + 5.2),center.y + radius * sinf(time + 5.2));'' – rptwsthi 2011-12-07 06:41:45