五个按钮保持等距圆周旋转

发布于 2024-10-26 18:52:43 字数 992 浏览 3 评论 0原文

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

我尝试过'

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);' 

旋转但它生成奇怪的路径..以三角形的方式.. ('地图','分享','幻灯片','网站') 是我的按钮..:P

I want to rotate five buttons on the circumference of an circle (circle with the center (120,120), which is actually center of square view i.e. 240*240) with radius 100. IS it possible to do so, with having interaction with the buttons which are rotating and a proper look.

i have tried '

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]];'

to calculate points and:

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);' 

to rotate but it generate weird path.. in triangular way..
('map','share','slideShow','website') ar my buttons.. :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2024-11-02 18:52:43

这太可爱了,不容错过,所以就在这里。这段代码仅限于一个按钮旋转,但我怀疑您很难将其扩展为多个按钮(提示:使用一个 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。

This was too cute to pass up so here it is. This code is limited to one button rotating but I doubt you'll have trouble extending it to take multiple (hint: use ONE CADisplayLink and rotate all buttons at once in that).

- (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];
}

We also need the actual "continueCircling:" method, which is simply:

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

I'm sure there are other more nifty ways to solve this, but the above works at least. :)

Edit: I forgot to mention, you will need to add QuartzCore framework and

#import <QuartzCore/QuartzCore.h> 

for CADisplayLink.

Edit 2: Found the PI constant (M_PI), so replaced 3.1415 with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文