【问题标题】:make a portion of a UIView become transparent使 UIView 的一部分变得透明
【发布时间】:2012-02-27 09:21:08
【问题描述】:

我有一个 UIView w/c 我在 viewForHeaderInSection 中作为视图返回。现在,我想在视图的右下方制作一个透明的小方块,以便可以看到单元格。我怎样才能实现这样的目标?我需要使用这个 CGContextSetBlendMode 吗?任何人都可以阐明这一点。

【问题讨论】:

  • 你可以让它全部透明,并添加一个 uiimageview 作为背景,并添加一个具有透明部分的 png。
  • 你能从结果中添加一个蓝图吗?很难想象。

标签: iphone objective-c ios uiview


【解决方案1】:

视图作为子视图插入到 SwitchViewController.view 中。因此,它们将始终出现在 SwitchViewControllers 视图上方。另一个视图被移除,1次只会出现一个视图

例如这里

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

视图默认出现在它的子视图后面,并且

insertSubview:atIndex:

不能将它放在视图本身的后面,因为视图不包含在它自己的子视图列表中。

【讨论】:

  • 让我知道这段代码是否有效。我遇到的问题与我仅通过堆栈解决的问题相同
【解决方案2】:

这是一个 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);
}

【讨论】:

    【解决方案3】:
    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];
    

    【讨论】:

      【解决方案4】:

      您需要为您的视图应用掩码。

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

      【讨论】:

      猜你喜欢
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多