繁体   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