CoreGraphics中的透明背景

Egi*_*gil 11 iphone core-graphics

我试图在CG中获得透明背景,但它一直是黑色的.

我有以下代码:

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        DLog(@">>> Alloc: DrawingCanvas [0x%X]", (unsigned int)self);
        [self setOpaque:NO];
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void) drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect frame = rect;
    int counter = 3;

    CGContextClearRect(context, frame);
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, frame);
}
Run Code Online (Sandbox Code Playgroud)

如何让此代码显示透明背景?

Ano*_*mie 4

通过该设置,只要您不设置为“否”,调用clearsContextBeforeDrawing您的方法时背景就应该是透明的。drawRect:删除 CGContextClearRect 和其他绘图调用。