[英]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.