2014-01-30 5 views
0

흰색 사각형 (또는 거의 사각형)이있는 녹색 원을 그려야합니다. 그러나 나는 CGContext 방법을 사용하는 방법을 이해할 수 없습니다. 여기 내 코드가있다. 먼저 원을 그려 채우고 내 원 안에 다른 무언가를 그려 넣고 싶습니다. 그러나 동일한 CGContextRef을 사용하여 프리미티브를 그릴 경우 CGContextSetFillColor 내 원의 색을 바꿉니다.석영 코어가 몇 개의 기본 도형을 그립니다

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextAddEllipseInRect(ctx, rect); 
    CGContextSetFillColor(ctx, CGColorGetComponents([self.color CGColor])); 
    CGContextFillPath(ctx); 

    switch (self.type) 
    { 
     case ETBRoundViewTypeBook: 
     { 
      CGRect bookRect = CGRectMake(CGRectGetMidX(rect) - 10, CGRectGetMidY(rect) - 12, 20, 24); 
      CGContextAddRect(ctx, bookRect); 
      CGContextSetFillColor(ctx, CGColorGetComponents([UIColor whiteColor].CGColor)); 
      CGContextFillPath(ctx); 
     } 
     case ETBRoundViewTypeList: 
     { 
      break; 
     } 
     case ETBRoundViewTypeTick: 
     { 
      break; 
     } 
     default: 
      break; 
    } 
} 

EDIT : 문제는 CGContextSetFillColorWithColorCGContextSetFillColor 대체함으로써 해결되었다.

답변

1

방금 ​​해 보았습니다.

-(void)drawRect:(CGRect)rect{ 
    CGContextRef ctx =UIGraphicsGetCurrentContext(); 
    CGContextAddEllipseInRect(ctx, rect); 
    CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor); 
    CGContextFillPath(ctx); 
    CGContextAddEllipseInRect(ctx, CGRectMake(30, 30, 40, 40)); 
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); 
    CGContextFillPath(ctx); 
} 
+0

'whiteColor'와 작동하지 않는 것처럼 보입니다. 'redColor'가 작동합니다. –

+0

아니요, 흰색으로도 작동합니다. 위의 drawRect 코드가 추가 된 UIView의 하위 클래스를 만들고 subView.It에 추가하여 테스트하십시오. – santhu

+0

내 사용자 정의보기에 하위보기를 추가해야합니까? UIView의 하위 클래스 중 하나만보기를 그릴 수 있습니까? –