Fastest way (performance wise) to darken UIView or CALayer

感情迁移 提交于 2019-12-08 05:04:32

问题


I have a UIView / CALayer which I would like to darken. How can I as fast as possible darken it? Is there any way to avoid blending?

Following alternatives is known to me

  • create a non-opaque CALayer and set background color and opacity and add it as a sublayer above main layer
  • same as above using UIView (can be a tad slower...?)
  • render view/layer to UIImage and doing drawing with CoreGraphics (is way too slow and content is dynamic/changing)

回答1:


Someone gave me a tip to have a dark background on the superview/superlayer and set an alpha of the view I want to darken. That way I don't need to add an extra layer/view.

The potential drawback with this is that the view you want to darken will be offscreen rendered if groupview opacity is on (on by default on iOS 7 and above).




回答2:


Create a non-opaque CALayer and set background color and opacity and add it as a sublayer above main layer




回答3:


I had to do something like this. For anyone curious for the actual code:

// 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];


来源:https://stackoverflow.com/questions/13970373/fastest-way-performance-wise-to-darken-uiview-or-calayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!