2011-11-14 2 views
2

이것은 어떻게 생겼는가? 나는 검은 배경을 맑게하고 싶지 않고 그것을 알아낼 수 없다.왜 "initi 프레임"이 검은 색으로 변하는가? initwithframe으로 CGRect 둥근 모서리 그리기

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Set Background Color & Border Color 
    CGColorRef bgColor = [UIColor scrollViewTexturedBackgroundColor].CGColor; 
    CGColorRef borderColor = [UIColor grayColor].CGColor; 

    CGRect Rect = self.bounds; 

    // Round Corners 
    roundCorners(context, Rect); 

    // Fill with bgcolor 
    fillBackgroundColor(context, Rect, bgColor); 

    // Add 1 px stroke 
    CGRect strokeRect = Rect; 
    strokeRect.size.height -= 1; 
    strokeRect = (strokeRect); 

    CGContextSetStrokeColorWithColor(context, borderColor); 
    CGContextSetLineWidth(context, 1.0); 
    CGContextStrokeRect(context, strokeRect); 


    // Overlay Rect to Tint "scrollViewTexturedBackgroundColor" UIColor 
    CGContextRef secondContext = UIGraphicsGetCurrentContext(); 

    // Set Background Color & Border Color 
    CGColorRef obgColor = [UIColor colorWithRed:255/255 green:255/255 blue:255/255 alpha:.3].CGColor; 

    // Round Corners 
    roundCorners(context, Rect); 

    // Fill with bgcolor 
    fillBackgroundColor(secondContext, Rect, obgColor); 

} 

코드가 함께 둥근 상자를 그립니다 내가 miniC에서 전화의 drawRect 곳

다음
loadView { 
    CGRect frame = CGRectMake(screenWidth - OFFSET, DEFAULT_PADDING, 140, 40); 
    miniC* mcView = [ [ [miniC alloc] initWithFrame:frame] autorelease]; 
    mcView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
    [[self view] addSubview:mcView]; 
} 

은 다음과 같습니다

enter image description here

는에는 loadView입니다 찾고있는 획이지만 새로운 둥근 b 뒤에는 블랙 박스가 있습니다. 소. 나는 여러 가지 다른 것들을 시도했지만 배경색을 분명하게 만드는 방법을 알아낼 수 없습니다. 또한 QuartzCore로이 작업을 수행 할 수 있다는 것을 알고 있지만 원하지 않습니다.

P. 나는 객관적으로 새로운 c 다.

EDIT :

공극 roundCorners (CGContextRef 컨텍스트 CGRect의 RECT) {

CGContextClearRect(context, rect); 

CGFloat c = INSET + CORNER_RADIUS; 

CGContextMoveToPoint(context, INSET, c); 

CGContextAddArcToPoint(context, INSET, INSET, c, INSET, CORNER_RADIUS);  
CGContextAddLineToPoint(context, rect.size.width - c, INSET); 
CGContextAddArcToPoint(context, rect.size.width - INSET, INSET, rect.size.width - INSET, c, CORNER_RADIUS); 
CGContextAddLineToPoint(context, rect.size.width - INSET, rect.size.height - c); 
CGContextAddArcToPoint(context, rect.size.width - INSET, rect.size.height - INSET, rect.size.width - c, rect.size.height - 

걸고 CORNER_RADIUS); CGContextAddLineToPoint (context, c, rect.size.height - INSET);

CGContextAddArcToPoint(context, INSET, rect.size.height - INSET, INSET, rect.size.height > - c, CORNER_RADIUS); 

CGContextClosePath(context); 

CGContextClip(context); 

CGContextSetFillColorWithColor(context, [UIColor scrollViewTexturedBackgroundColor].CGColor); 
CGContextFillRect(context, CGRectMake(0, 0, rect.size.width - INSET, rect.size.height - INSET)); 

}

무효 fillBackgroundColor (CGContextRef 컨텍스트, CGRect의 RECT, CGColorRef bgColor가) {

CGContextSetFillColorWithColor(context, bgColor); 
CGContextFillRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height)); 

}

+0

뷰를 불투명하게 만드는 것을 기억 했는가? –

+0

분명히하지 않았습니다. 내가 이것을 어디에서 설정할 것입니까? – morcutt

+0

'mcView.opaque = NO'를 loadView에 추가하면 그 내용을 확인할 수 있습니다. 그리고 그 것이 유일한 문제라면, 그것을 Paul의 initWithFrame으로 이동하십시오. 너를 위해 썼다. –

답변

5

당신은 당신의 UIView의 배경색을 설정해야

예 :

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

당신이 언어 등

이를 제거 할 수 있도록하는 방법의 인수로 rect을 통과되는대로이 할당 CGRect Rect = self.bounds;는 superflous되는 새로운 말을 특히 고려해야 할 몇 가지 다른 것들.

대문자로 시작하는 항목이 일반적으로 상수이기 때문에 변수에 대한 이름이 CGRect Rect 인 것은 Objective-C에서 훌륭하지 않습니다. 따라서이 문자는 혼동을 줄 수 있으며 나중에 문제가 발생할 수 있습니다.

이 할당 strokeRect = (strokeRect);이에 잘 작업을 수행, superflous입니다 그것의 자신의 strokeRect.size.height -= 1;

내가 더 핵심 그래픽 전문가는 아니지만하지만 난 당신이 전에했던 같은 컨텍스트를 반환이 CGContextRef secondContext = UIGraphicsGetCurrentContext();을 가정하고 잘못 될 수 있습니다.

+0

이미 roundedCorners에서 호출했습니다. – morcutt

+0

그 방법을 보여 드릴까요? –

+0

예, 방금 추가했습니다. 나는 처음부터 가지고 있어야한다. – morcutt

1

CGContextClearRect는 매우 까다 롭습니다. 컨텍스트는 비어있게되므로 비어있는 경우가 아니라면 사용하면 안됩니다. 중요한 점은 뷰의 backgroundColor가 100 % 불투명 한 색상이 아니어야합니다. 알파가 1보다 작아야합니다. 그렇지 않으면 투명한 색상 대신 검정색으로 표시됩니다.

코드가 너무 많아서 혼란스러워하고 도움을 필요로하는 사람들에게 삶을 어렵게 만듭니다. 아주 단순한 개념 증명으로 시작하는 것이 가장 좋습니다. 여기 CGContextClearRect 클립핑을 사용하여 라운드 RECT 자체 도료 매우 단순한 관점이다 :

- (void) configure { 
    self.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.99]; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self configure]; 
    } 
    return self; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder: aDecoder]; 
    if (self) { 
     [self configure]; 
    } 
    return self; 
} 

- (void) drawRect: (CGRect) rect { 
    CGContextRef con = UIGraphicsGetCurrentContext(); 
    CGPathRef p = [[UIBezierPath bezierPathWithRoundedRect:rect 
               cornerRadius:10] CGPath]; 
    CGContextAddRect(con, rect); 
    CGContextAddPath(con, p); 
    CGContextEOClip(con); 
    CGContextClearRect(con, rect); 
} 

결과 적색 둥근 RECT이다. 하지만 ... 첫 번째 줄에서 .99를 1로 변경하면 사진의 경우와 마찬가지로보기의 구석이 검은 색으로 나옵니다. 이것은 CGContextClearRect를 사용할 때 불투명 뷰 배경색의 중요성을 보여줍니다. (나는 이것을 버그라고 생각하지만, 여기도 여기에 없으며, 오랜 시간 동안이 방법을 사용했고, 변화가있을 것이라고 기대하지 않는다.)