iphone - CoreGraphics 中的透明背景

标签 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);
}

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

最佳答案

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

关于iphone - CoreGraphics 中的透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5148084/

相关文章:

iphone - 除了 Siphon 之外,iPhone 是否还有其他 SIP 实现?

ios - 定义大于 iPhone 6 屏幕尺寸?

iphone - CGAffineTranformRotate atan2 不准确

ios - Crossfade 多个 UIViews 没有背景通过 alpha 偷看?

ios - 圆角 : How to calculate Fillet radius?

iphone - 应用程序提交需要每年续订吗?

iphone - 缺少 Facebook oauth/access_token

iphone - 数据源和 TableView 的问题

macos - 使用 kCGBlendModeClear 在 Mac OS X 上实现橡皮擦描边

iphone - 如何为 UILabel 的文本而不是背景添加渐变?