2011-03-25 1 views
1

반지름 100의 원 (원 (중심이 120,120), 실제로 정사각형의 중심 즉 240 * 240)의 원주에 5 개의 버튼을 회전시키고 싶습니다. IS 가능하고 회전하고있는 버튼과 상호 작용을하는 것. ','공유 '지도 :원형 회전 동등한 거리를 유지하는 다섯 개의 버튼

난'

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

가 회전하지만 이상한 경로를 생성 .. 삼각 방법으로 .. (

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

포인트를 계산하기 위해 '시도 , 'slideShow', 'website') ar 내 버튼 .. : P

+1

"angl"방사형입니까? 그건 그래야만 해. radAngl = angl/180 * Pi. 또한 이전 좌표에 다음 반복을 적용하지 마십시오. 각도를 증가시키고 초기 위치를 사용하여 위치를 계산하십시오. – Krumelur

+0

아니야, 내가 하나씩 감소하는 각도 야. – rptwsthi

+0

@vladimir : 어떻게 그랬는지 말해 줄 수 있니?. :) – rptwsthi

답변

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 프레임 워크와 CADisplayLink에 대한

#import <QuartzCore/QuartzCore.h> 

을 추가해야합니다.

편집 2 : PI 상수 (M_PI)가 있으므로 3.1415로 교체하십시오.

+0

당신은 내 영웅 .. 고맙습니다. 지금 더 노력할 것입니다 .. – rptwsthi

+0

그래, 작동해야하는 것처럼 들리네. 동일한 거리를 원한다면 그들 사이의 각 "단계"가 2 * PI의 제수가되기를 원할 것입니다. PI는 상수로서 어딘가에 있지만 3.1415를 사용하면 충분히 가깝습니다. – Kalle

+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 (시간 + 2.6), center.y + radius * sinf (시간 + 2.6)); e.center = CGPointMake (center.x + radius * cosf (시간 + 3.9), center.y + radius * sinf (시간 + 3.9)); f.center = CGPointMake (center.x + radius * cosf (time + 5.2), center.y + radius * sinf (time + 5.2)); – rptwsthi