为什么我的"initiwithframe"变黑?使用initwithframe进行CGRect圆角绘图

mor*_*utt 2 iphone objective-c ipad ios

这就是它的样子.我想让黑色背景清晰,似乎无法弄明白.

在此输入图像描述

这是loadView:

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

这是我在miniC中调用drawRect的地方:

- (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);

}
Run Code Online (Sandbox Code Playgroud)

代码用我正在寻找的笔划绘制圆形框,但在新的圆形框后面有一个黑框.我一直在尝试一些不同的东西,但似乎无法弄清楚如何使背景颜色清晰.另外,我知道我可以用QuartzCore做到这一点,但我不想这样做.

PS我是Objective-c的新手.

编辑:

void roundCorners(CGContextRef context,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 -
Run Code Online (Sandbox Code Playgroud)

INSET,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));
Run Code Online (Sandbox Code Playgroud)

}

void fillBackgroundColor(CGContextRef context,CGRect rect,CGColorRef bgColor){

CGContextSetFillColorWithColor(context, bgColor);
CGContextFillRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
Run Code Online (Sandbox Code Playgroud)

}

Pau*_*l.s 5

你需要设置你的背景颜色 UIView

例如

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

其他一些需要考虑的因素,特别是当你说你的语言新手等时.

CGRect Rect = self.bounds;当您rect作为方法参数传入时,此赋值是超级的,因此您可以删除它.

CGRect Rect在Objective-C中,变量的名称并不是很好,因为以大写字母开头的东西通常是常量,所以这可能会让人感到困惑并在以后引起一些问题.

这项任务strokeRect = (strokeRect);是超级的,这可以很好地完成它自己的工作strokeRect.size.height -= 1;

我可能是错的我不是核心图形专家,但我假设这CGContextRef secondContext = UIGraphicsGetCurrentContext();将返回你以前的相同背景.