2014-07-08 3 views
-1

원 안에 호를 그리고 호를 채우려고합니다. 다음은 내가 사용하고있는 코드입니다 :목표 C - 원 안에 다중 호를 그립니다.

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     CGContextAddEllipseInRect(ctx, rect); 
     CGContextSetFillColor(ctx, CGColorGetComponents([[clrArray objectAtIndex:0] CGColor])); 
     CGContextFillPath(ctx); 

     CGFloat radius = 12.5; 

     CGFloat startAngle1 = DegreesToRadians(0); 
     CGFloat endAngle1 = DegreesToRadians(135); 

     CGFloat startAngle2 = DegreesToRadians(135); 
     CGFloat endAngle2 = DegreesToRadians(270); 

     //draw arc 
     CGPoint center = CGPointMake(radius,radius); 

     //Arc 1 
     CGContextAddArc(ctx, 
         center.x, 
         center.y, 
         radius, 
         startAngle1, 
         endAngle1, 
         YES); 
     [(UIColor*)[clrArray objectAtIndex:1] set]; 
     CGContextFillPath(ctx); 

     //Arc 2 
     CGContextAddArc(ctx, 
         center.x, 
         center.y, 
         radius, 
         startAngle2, 
         endAngle2, 
         YES); 
     [(UIColor*)[clrArray objectAtIndex:2] set]; 
     CGContextFillPath(ctx); 

오른쪽 그림이 아닙니다. 만약 하나의 호만 제공한다면 각도가 180보다 작 으면 오른쪽 대담한 것도 아닙니다. 내가 뭘 잘못하고있어? 편집

: 여기 내가

+0

를 사용하여 다른 솔루션을있어이 이미지의 시작 각도 0과 끝 각도 (45)를 사용 enter image description here

을 무슨 일이 일어나고 있는지의 이미지입니다 ** 어떤 의미에서 "그리기가 옳지 않은가"** –

+0

그것은 그리기는하지만 틀리게 그린다. 내가 천사에게 0을 주었던 것은 x = 0 y = center.y이고 135는 원의 각도 일 것입니다. 하지만 대신 오른쪽 하단 모서리에 그려져 있습니다. – Hassy

+0

그리고 끝점을 주면 서클 끝의 절반에 정확히 그려지며 시작점은 내가 언급 한 것과 같을 것입니다 – Hassy

답변

0

는 베 지어 패스

CGContextRef ctx = UIGraphicsGetCurrentContext(); 

     CGFloat radius = 50.0/2.0; 
     CGPoint center = CGPointMake(radius,radius); 

     CGFloat startAngle1 = DegreesToRadians(0); 
     CGFloat endAngle1 = DegreesToRadians(120); 

     CGFloat startAngle2 = DegreesToRadians(120); 
     CGFloat endAngle2 = DegreesToRadians(240); 

     CGContextSaveGState(ctx); 

     UIBezierPath* clip1 = [UIBezierPath bezierPathWithArcCenter:center 
                  radius:radius 
                 startAngle:startAngle1 
                  endAngle:endAngle1 
                 clockwise:YES]; 
     [clip1 addLineToPoint:center]; 
     [clip1 closePath]; 
     [clip1 addClip]; 

     UIBezierPath *arc1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)]; 
     [[UIColor blackColor] set]; 
     [arc1 fill]; 

     CGContextRestoreGState(ctx); 

     CGContextSaveGState(ctx); 

     UIBezierPath* clip2 = [UIBezierPath bezierPathWithArcCenter:center 
                  radius:radius 
                 startAngle:startAngle2 
                  endAngle:endAngle2 
                  clockwise:YES]; 
     [clip2 addLineToPoint:center]; 
     [clip2 closePath]; 
     [clip2 addClip]; 

     UIBezierPath *arc2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)]; 
     [[UIColor orangeColor] set]; 
     [arc2 fill]; 

     CGContextRestoreGState(ctx); 
관련 문제