简体   繁体   English

使 UIView 的一部分变得透明

[英]make a portion of a UIView become transparent

I have a UIView w/c I returned as view in my viewForHeaderInSection.我有一个 UIView w/c 我在 viewForHeaderInSection 中作为视图返回。 Now, I want to make a small transparent square on the lower right of the view so the cells will be seen through.现在,我想在视图的右下角制作一个透明的小方块,以便可以看到单元格。 How can I achieve something like this?我怎样才能实现这样的目标? Do I need to use this CGContextSetBlendMode?我需要使用这个 CGContextSetBlendMode 吗? can anyone shed light on this.任何人都可以阐明这一点。

Here is a UIView subclass solution.这是一个 UIView 子类解决方案。 In order for this to work you must leave the superviews backgroundColor property as nil witch is the default value.为了使其工作,您必须将 superviews backgroundColor 属性保留为 nil 女巫是默认值。 If this UIView-subclass is in the storyboard then make sure you set the background color to "Clear color" in the attributes inspector.如果此 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];

or override the drawRect method and add the below lines of code at the end或覆盖 drawRect 方法并在最后添加以下代码行

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

You need to apply mask for your View.您需要为您的视图应用蒙版。

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

The view are inserted as subviews inside of SwitchViewController.view.该视图作为子视图插入 SwitchViewController.view 中。 So, they will always appear above the SwitchViewControllers view.因此,它们将始终出现在 SwitchViewControllers 视图上方。 The other view is removed, Only one view will be appeared at 1 time另一个视图被移除,1 次只会出现一个视图

eg Here例如这里

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

view appears behind its subview by default, and默认情况下,视图显示在其子视图后面,并且

insertSubview:atIndex:

can't place it behind the view itself, as a view is not contained in the list of its own subviews.不能将它放在视图本身后面,因为视图不包含在其自己的子视图列表中。

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

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