簡體   English   中英

使 UIView 的一部分變得透明

[英]make a portion of a UIView become transparent

我有一個 UIView w/c 我在 viewForHeaderInSection 中作為視圖返回。 現在,我想在視圖的右下角制作一個透明的小方塊,以便可以看到單元格。 我怎樣才能實現這樣的目標? 我需要使用這個 CGContextSetBlendMode 嗎? 任何人都可以闡明這一點。

這是一個 UIView 子類解決方案。 為了使其工作,您必須將 superviews backgroundColor 屬性保留為 nil 女巫是默認值。 如果此 UIView 子類在故事板中,請確保在屬性檢查器中將背景顏色設置為“清除顏色”。

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIColor *blackBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
    CGContextSetFillColorWithColor(ctx, blackBackgroundColor.CGColor);
    CGContextFillRect(ctx, rect);
    CGRect transparentPart = CGRectMake(10, 10, 120, 80);
    CGContextClearRect(ctx, transparentPart);
}
CALayer *layer = [[CALayer alloc] init];
[layer setFrame:yourBoundsInView];

[[yourView layer] setMask:layer];

或覆蓋 drawRect 方法並在最后添加以下代碼行

UIBezierPath *seeThrough = [UIBezierPath bezierPathWithRect:seeThroughRect];
[[UIColor colorWithWhite:1 alpha:0] set];
[seeThrough fillWithBlendMode:kCGBlendModeSourceIn alpha:1];

您需要為您的視圖應用蒙版。

教程: https : //medium.com/@peteliev/layer-masking-for-beginners-c18a0a10743

該視圖作為子視圖插入 SwitchViewController.view 中。 因此,它們將始終出現在 SwitchViewControllers 視圖上方。 另一個視圖被移除,1 次只會出現一個視圖

例如這里

[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];

默認情況下,視圖顯示在其子視圖后面,並且

insertSubview:atIndex:

不能將它放在視圖本身后面,因為視圖不包含在其自己的子視圖列表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM