繁体   English   中英

如何在 UIView 中绘制透明度?

[英]How to draw transparency in UIView?

我很清楚这个问题已经被问过很多次了,但显然有些不同。 我在 UIImageView 中的脸上绘制矩形,所以我需要矩形是透明的。

据我所知,我遵循所有建议在视图中绘制透明背景(例如清除当前图形上下文的矩形)。 我还将视图设置为不透明,并将 backgroundColor 设置为 clearColor。 是否还需要其他步骤?

-(instancetype)init
{
    self = [super init];
    if ( self ) {
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];
        self.clearsContextBeforeDrawing = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect];

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] setFill];

    CGContextClearRect(ctx, rect);

    [self.rectColor setStroke];
    path.lineWidth = 5.0;
    [path stroke];
}

@end

我的输出示例

我需要背景是透明的,这个视图默认显示为黑色。 我试图直接清除 CGContext 的矩形,因为我听说这通常是解决方案。

我猜测您的init方法从未被调用(毕竟,这不是指定的初始化程序),因此您的代码从未运行,因此创建的视图是不透明的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM