【问题标题】:How to make CALayer border alpha value different with the bgcolor如何使 CALayer 边框 alpha 值与 bgcolor 不同
【发布时间】:2015-02-12 04:26:21
【问题描述】:

我有一个 UIView,它的图层具有 0.5 的 alpha 值和白色背景色。而且我需要一个具有相同白色但 alpha 值为 1 的边框。不幸的是,AFAIK 边框的 alpha 值始终与 bgcolor 相同。所以我的问题是:

  1. 我可以使边框的 alpha 与 bgcolor 不同吗?
  2. 如果我不能,您有什么建议吗?

【问题讨论】:

    标签: ios objective-c iphone quartz-core


    【解决方案1】:

    试试这个。这应该只更改边框颜色和 alpha。

    [self.yourView.layer setBorderColor:[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha:1.0].CGColor]; 你当然可以将 RGB 值更改为你需要的任何值。

    【讨论】:

      【解决方案2】:

      我可以使边框的 alpha 与 bgcolor 不同吗?

      是的。你可以这样做:

      CALayer * firstLayer = [[CALayer alloc] init];
      
      [firstLayer setFrame:CGRectMake(100, 100, 100, 100)];
      
      firstLayer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
      firstLayer.borderWidth = 3.0f;
      firstLayer.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5].CGColor;
      

      另一种方法是在另一个相同大小的层中放置一个层。给外层你想要的边框厚度,并确保它的不透明度为 100%。第二层应该有分数不透明度:

      CALayer * firstLayer = [[CALayer alloc] init];
      CALayer * secondLayer = [[CALayer alloc] init];
      
      [firstLayer setFrame:CGRectMake(100, 100, 100, 100)];
      [secondLayer setFrame:CGRectMake(0, 0, 100, 100)];
      [firstLayer addSublayer:secondLayer];
      
      firstLayer.borderColor = [UIColor redColor].CGColor;
      firstLayer.borderWidth = 1.0f;
      
      secondLayer.backgroundColor = [UIColor redColor].CGColor;
      secondLayer.opacity = 0.8f;
      
      [self.view.layer addSublayer:firstLayer];
      

      【讨论】:

      • 谢谢。这是一个更完整的答案。
      【解决方案3】:

      只需使用:

      view.layer.borderColor = [[UIColor redColor] colorWithAlphaComponent:0.6].CGColor;
      view.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];
      

      【讨论】:

        猜你喜欢
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        • 2016-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2014-03-22
        相关资源
        最近更新 更多