2014-07-07 4 views
2

4 개의 컬러 세그먼트 (그라디언트가 아닌 4 가지 색상)가있는 막대를 그리려하고 있습니다. 그 4 UIColors 배열에 저장되어 있습니다 (나는 디버그하고 그 색상이 제대로 설정되어 있는지 확인). 어떤 이유로이 루프는 첫 번째 색만 정확한 너비로 그립니다. 이 클래스의 UIView를 상속코어 그래픽이 적용된 멀티 컬러 라인

TopBar* bar = [[TopBar alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 20)]; 
[self.view addSubview:bar]; 

를 통해 호출되고있는의 drawRect는 다음과 같다 :

-(void)drawRect:(CGRect)rect{ 
NSLog(@"draw rect"); 
[super drawRect:rect]; 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 

NSInteger fullBarWidth = self.frame.size.width; 
NSInteger individualBarWidth = fullBarWidth/self.barColors.count; 
NSInteger currentColorIndex = 0; 

CGContextSetLineWidth(ctx, self.frame.size.height); 
CGContextSetLineCap(ctx, kCGLineCapButt); 


for (UIColor *color in self.barColors) 
{ 
    NSInteger currentDrawLoc = currentColorIndex * individualBarWidth; 
    currentColorIndex++; 

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, currentDrawLoc, 0); 
    CGContextAddLineToPoint(ctx, individualBarWidth, 0); 
    CGContextSetStrokeColorWithColor(ctx,color.CGColor); 
    CGContextStrokePath(ctx); 
} 
} 

어떤 도움이 줄

답변

0

을 감상 할 수있다 :

CGContextAddLineToPoint(ctx, individualBarWidth, 0); 

다음과 같이 말하고자하는 것 :

CGContextAddLineToPoint(ctx, currentDrawLoc + individualBarWidth, 0); 
+0

Perfect! 정말 고마워. 내가 5 분 안에 할 수 있으면 이걸 받아 들일거야. – user3661142