ios UIView:不透明 vs. alpha vs. 不透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8520434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:53:47  来源:igfitidea点击:

UIView: opaque vs. alpha vs. opacity

iphoneobjective-ciosuiview

提问by Besi

How do opaquealphaand the opacity of the background work together for a UIView and what are the differences between them?

opaquealphaUIView 背景的不透明度和不透明度如何协同工作,它们之间有什么区别?

UIView http://i.minus.com/jb2IP8TXbYTxKr.png

UIView http://i.minus.com/jb2IP8TXbYTxKr.png

回答by deanWombourne

opaquemeans don't draw anythingunderneath, even if you are transparent.

opaque意味着不要在下面画任何东西,即使你是透明的。

The background color's alpha only affects the background color's transparency, not anything else drawn on the view.

背景颜色的 alpha 只影响背景颜色的透明度,不会影响视图上绘制的任何其他内容。

alphaaffects everything drawn on the view.

alpha影响在视图上绘制的所有内容。



The opaque property can give you a speed increase - if you know that your view will neverhave transparency you can set this to YESand when iOS renders your view it can make some performance optimisations and render it faster. If this is set to NOiOS will have to blend your view with the view underneath, even if it doesn't happen to contain any transparency.

opaque 属性可以提高速度——如果你知道你的视图永远不会有透明度,你可以设置它YES,当 iOS 渲染你的视图时,它可以进行一些性能优化并更快地渲染它。如果将其设置为NOiOS,则必须将您的视图与下面的视图混合,即使它不包含任何透明度。

The alpha will also affect the alpha of the backround color i.e. if the background color is 0.5 transparent and the alpha is also 0.5, this has the effect of making the background view's alpha 0.25 (0.5 * 0.5).

alpha 也会影响背景颜色的 alpha,即如果背景颜色是 0.5 透明并且 alpha 也是 0.5,这会使背景视图的 alpha 为 0.25 (0.5 * 0.5)。

回答by Mikolaj

To the very good answerby deanWombourne it's worth to add that, unless you don't draw your own content using the drawRect: method, the opaque property has no effect.

对于deanWombourne的非常好的回答,值得补充的是,除非您不使用 drawRect: 方法绘制自己的内容,否则 opaque 属性无效。

Apple's doc:

苹果的文档

You only need to set a value for the opaque property in subclasses of UIView that draw their own contentusing the drawRect: method. The opaque property has no effect in system-provided classessuch as UIButton, UILabel, UITableViewCell, and so on.

您只需要在 UIView 的子类中为 opaque 属性设置一个值,这些子类使用 drawRect: 方法绘制自己的内容。opaque 属性在系统提供的类中不起作用,例如 UIButton、UILabel、UITableViewCell 等。

If you draw your own content, keep in mind, that opaque is just a hint

如果您绘制自己的内容,请记住,不透明只是一个提示

This property provides a hint to the drawing systemas to how it should treat the view.

该属性为绘图系统提供了一个关于它应该如何处理视图的提示。

and some more guidelines from the same Apple's doc:

以及来自同一 Apple 文档的更多指南:

If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.

如果视图不透明并且未填充其边界或包含完全或部分透明的内容,则结果是不可预测的。如果视图完全或部分透明,则应始终将此属性的值设置为 NO。