2013-05-21 1 views
0

두 개의 동심원이 있는데 그 사이의 거리는 일정합니다. 원형 부분 (검은 동그라미 안쪽 포함)을 만질 때 얻은 각도에 따라 가장 바깥 쪽 원의 원주 좌표를 얻습니다.원의 원주 좌표를 알고있을 때 왼쪽 상단 좌표를 얻는 방법은 무엇입니까?

이것은 UIControl의 하위 클래스이며 주위에 점을 찍는 방법을 사용합니다.

정확한 각도를 성공적으로 얻었으므로 가장 바깥 쪽 원의 원주에 정확한 점을 얻습니다.

그러나 동심원의 지름 = distance_between_concentric_circles + 2 * offset과 같은 버튼을 배치하고 싶습니다.

이 오프셋은 버튼 모서리가 동심원 영역 밖으로 나오도록 사용됩니다.

enter image description here

아래 이미지마다처럼은 원형 경로를 따라 이동한다은 그 버튼을 이동.

내가 그리기를 원하지 않기 때문에 uibutton 및 이미지보기를 사용하고 가장 바깥 쪽 원형의 원주와 UIButton의 크기를 기반으로 왼쪽 위 좌표를 얻기가 어렵다. .

버튼을 움직일 수는 있지만 원형 경로에는 정확하게 놓을 수 없습니다.

왼쪽 위 좌표를 가져와 uibutton의 프레임을 설정하는 방법이 있는지 알려주지는 못합니다.

그리기없이 이것을하고 싶습니다.

어떤 도움을 주시면 감사하겠습니다.

답변

1

왼쪽 상단을 걱정하지 않고 버튼의 center 속성 (frame 속성 대신)을 설정하는 것이 더 쉽습니다. 왼쪽 상단의 오프셋을 계산하는 것은 어렵지 않지만 center 속성을 조정하는 것이 훨씬 더 직관적입니다. IMHO.당신이 center 또는 frame 좌표를 조정하지 않으려면


또는, 당신은 화면의 어떤 점에 대해 버튼을 회전 석영 2D를 사용할 수 있습니다

  • 변경 버튼의 anchorPoint;

  • 은 회전 (때문에 당신이 버튼이 적절하게 그 position 오프셋됩니다 anchorPoint 설정 한)되는 대한 포인트가 될 버튼의 position을 설정; 및

  • 해당 고정 점에 대해 회전하십시오.

그래서, 당신 link the QuartzCore.framework to your project 경우 다음 수 :

#import <QuartzCore/QuartzCore.h> 

@interface ViewController() 

@property (nonatomic, strong) CADisplayLink *displayLink; 
@property (nonatomic) CFTimeInterval startTime; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self rotateButtonAroundPoint]; 
} 

- (void)rotateButtonAroundPoint 
{ 
    // the point about which I'm rotating and at what radius 

    CGPoint center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0); 
    CGFloat radius = 100.0; 

    // now configure the button's layer accordingly 

    self.button.layer.anchorPoint = CGPointMake(0.5, 0.5 + radius/self.button.frame.size.height); 
    self.button.layer.position = center; 

    // just so I can see what I'm rotating this around 

    [self addCircleAt:center radius:5.0 color:[UIColor redColor]]; 

    // turn on display link to animate it (better than `NSTimer` for animations) 

    [self startDisplayLink]; 
} 

- (void)addCircleAt:(CGPoint)center radius:(CGFloat)radius color:(UIColor *)color 
{ 
    CAShapeLayer *layer = [CAShapeLayer layer]; 
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2.0 * M_PI clockwise:YES]; 

    layer.path = [path CGPath]; 
    layer.fillColor = [color CGColor]; 
    [self.view.layer addSublayer:layer]; 
} 

- (void)startDisplayLink 
{ 
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)]; 
    self.startTime = CACurrentMediaTime(); 
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
} 

- (void)stopDisplayLink 
{ 
    [self.displayLink invalidate]; 
    self.displayLink = nil; 
} 

- (void)handleDisplayLink:(CADisplayLink *)displayLink 
{ 
    CFTimeInterval elapsed = CACurrentMediaTime() - _startTime; 

    self.button.transform = CGAffineTransformMakeRotation(elapsed * 2.0 * M_PI/5.0); // duration = 5.0 seconds 
} 

난 그냥 center/frame을 변경하거나 (오히려이 회전 이상) 번역을하는 것은 계산적으로 덜 비싼있을 것으로 생각하지만, 단추가 실제로 회전 할 때이 단추를 실제로 회전 시키려면이 옵션을 사용하고 특정 우아함을 누립니다 (단지 positionanchorPoint으로 설정하고 회전하면 데카르트 좌표가 전혀 걱정되지 않습니다).

+1

고맙습니다. 반경을 변경하여 동심원 사이에서 원주에 좌표를 가져오고 uibutton의 중심으로 설정하고 작동하도록 반경을 변경했습니다. – user1899840

0

원점 (r*cos(theta),r*sin*(theta)) 떨어져 원점으로부터로 나타낸 바와 같이 중심으로 원의 원주상의 임의의 지점은, 원주상의 임의의 지점은 (x+r*cos(theta),y+r*sin*(theta)) (x,y)이 원의 중심, 그래서 (x+r*cos(theta),y+r*sin*(theta))를 계산 버튼을 설정할 것이다 것 center

관련 문제