【问题标题】:Fastest way (performance wise) to darken UIView or CALayer使 UIView 或 CALayer 变暗的最快方法(性能方面)
【发布时间】:2012-12-20 10:45:12
【问题描述】:

我有一个 UIView / CALayer 我想变暗。我怎样才能尽快变暗呢?有什么方法可以避免混合?

我知道以下替代方案

  • 创建一个不透明的 CALayer 并设置背景颜色和不透明度,并将其作为子层添加到主层之上
  • 与上面使用 UIView 相同(可能会慢一点...?)
  • 将视图/图层渲染到 UIImage 并使用 CoreGraphics 进行绘图(太慢而且内容是动态/变化的)

【问题讨论】:

  • 我想你已经有了答案。
  • 所以没有其他更好/更好的选择?
  • 我认为可能不是。带有填充的图层非常快。如果您确实有性能问题可以重新访问。你也仅限于我认为的基本混合模式;我认为没关系。
  • 是的,不需要花哨的混合模式。只是正常的。

标签: uiview calayer alphablending


【解决方案1】:

有人给了我一个提示,让我在超级视图/超级图层上设置深色背景,并设置我想要变暗的视图的 alpha。这样我就不需要添加额外的图层/视图。

这样做的潜在缺点是,如果 groupview 不透明度打开(iOS 7 及更高版本默认打开),您想要变暗的视图将在屏幕外渲染。

【讨论】:

  • 如果没有别的,不用添加额外的层就好了。
【解决方案2】:

创建一个不透明的 CALayer 并设置背景颜色和不透明度,并将其添加为主图层上方的子图层

【讨论】:

    【解决方案3】:

    我不得不做这样的事情。对于任何对实际代码感兴趣的人:

    // assuming the view you're trying to darken is called 'mainUIView'
    // make the dark layer the same size as the view you're overlaying
    UIView *darkBackgroundView = [[UIView alloc] initWithFrame:mainUIView.frame];
    CALayer *darkenLayer = darkBackgroundView.layer;
    // background color
    darkenLayer.backgroundColor = [UIColor blackColor].CGColor;
    // transparency (0 is transparent, 1 is solid)
    // you can adjust this for the level of darkness you prefer
    darkenLayer.opacity = 0.75f;
    [mainUIView addSubview:darkBackgroundView];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多