2013-03-05 207 views
7

我想在我的iOS應用程序中創建下面的圈子。我知道如何製作圈子,但我不完全確定如何獲得弧形點。它必須在代碼中而不是圖像。以下是我目前擁有的代碼。iOS繪圖圈

enter image description here

- (void)drawRect:(CGRect)rect 
{ 
    CGPoint point; 
    point.x = self.bounds.origin.x + self.bounds.size.width/2; 
    point.y = self.bounds.origin.y + self.bounds.size.height/2; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGRect circle = CGRectMake(point.x/2,point.y-point.x/2,point.x,point.x); 

    CGContextAddEllipseInRect(context, circle); 

    CGContextStrokePath(context); 

    for (int i = 0; i<8; i++) { 
     CGRect circleMini = CGRectMake(??????,??????,point.x/4,point.x/4); 

     CGContextAddEllipseInRect(context, circleMini); 
     CGContextStrokePath(context); 
    } 

} 

更新到ANSWER

float cita = 0; 
for (int i = 0; i<8; i++) { 

    CGPoint pointCir = CGPointMake(point.x/2 + radius * cos(cita) , (point.y-point.x/2) + radius * sin(cita)); 
    CGRect circleMini = CGRectMake(pointCir.x,pointCir.y,radius/4,radius/4); 

    CGContextAddEllipseInRect(context, circleMini); 
    CGContextStrokePath(context); 
    cita += M_PI/4.0; 
} 

enter image description here

回答

5

如果(X,Y)爲中心,r爲你的大圓圈的半徑的中心第i個外部圓圈將爲:

center(i) = (x + r * cos(cita) , y + r * sin(cita)) 

開始CITA在0和增加它的PI/4弧度的下一個圓(或45度)

工作實現

CGFloat cita = 0; 
CGFloat bigCircleRadius = point.x/2.0; 
CGFloat smallCircleRadius = bigCircleRadius/4.0; 
for (int i = 0; i < 8; i++) { 

    CGPoint smallCircleCenter = CGPointMake(point.x + bigCircleRadius * cos(cita) - smallCircleRadius/2.0 , point.y + bigCircleRadius * sin(cita) - smallCircleRadius/2.0); 
    CGRect smallCircleRect = CGRectMake(smallCircleCenter.x,smallCircleCenter.y,smallCircleRadius,smallCircleRadius); 

    CGContextAddEllipseInRect(context, smallCircleRect); 
    CGContextStrokePath(context); 
    cita += M_PI/4.0; 
} 

編輯:新增實施,並更名爲變量。

+0

它不工作。我只是得到了大圈子。你能看到我如何在上面實現它。我做錯了嗎? – BDGapps 2013-03-05 23:16:26

+0

你應該使用cita + = M_PI/4.0,而不是cita = M_PI/4,根據你的實現,不是圓心(point.x/2,point.y-point.x/2)? – ikaver 2013-03-05 23:27:58

+0

我更新了上面的代碼,但同樣的問題 – BDGapps 2013-03-05 23:33:28

0

 

- (void)drawRect:(CGRect)rect 
{ 
    const NSUInteger kNumCircles = 8u;

CGFloat height = CGRectGetHeight(rect); CGFloat smallCircleRadius = height/10.0f; CGRect bigCircleRect = CGRectInset(rect, smallCircleRadius/2.0f, smallCircleRadius/2.0f); CGFloat bigCircleRadius = CGRectGetHeight(bigCircleRect)/2.0f; CGPoint rectCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0f); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGContextAddEllipseInRect(context, bigCircleRect); CGContextStrokePath(context); CGFloat alpha = 0; for (NSUInteger i = 0; i < kNumCircles; i++) { CGPoint smallCircleCenter = CGPointMake(rectCenter.x + bigCircleRadius * cos(alpha) - smallCircleRadius/2.0f , rectCenter.y + bigCircleRadius * sin(alpha) - smallCircleRadius/2.0f); CGRect smallCircleRect = CGRectMake(smallCircleCenter.x,smallCircleCenter.y,smallCircleRadius,smallCircleRadius); CGContextAddEllipseInRect(context, smallCircleRect); CGContextStrokePath(context); alpha += M_PI/(kNumCircles/2.0f); }

}