[BS-16] 尽量将View的Opaque属性设置为YES(默认就是YES)

时间:2022-05-02 08:08:03

尽量将View的Opaque属性设置为YES(默认就是YES)

UIView控件都有一个Opaque属性,如果不会更改view的透明度,那么应该将其opaque属性设置为YES。为什么要这样做呢?其实是有一定的原因的,一起来看一下。这样设置ios可以让系统以最优的方式来绘制view。opaque属性可以在Interface Builder或代码中设置。 

 
苹果的官方文档对opaque属性有如下解释:

This property provides a hint to the drawing system as to how it should treat the view. If set to YES, the drawing system treats the view as fully opaque, which allows the drawing system to optimize some drawing operations and improve performance. If set to NO, the drawing system composites the view normally with other content. The default value of this property is YES.

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. 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.

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

(opaque属性提示绘制系统如何处理view。如果opaque设置为YES,绘图系统会将view看为完全不透明,这样绘图系统就可以优化一些绘制操作以提升性能。如果设置为NO,那么绘图系统结合其它内容来处理view。默认情况下,这个属性是YES。) 

 
如果屏幕是静止的,那么这个opaque属性的设置与否不是一个大问题。但是,如果view是嵌入到scroll view中的,或者是复杂动画的一部分,不将设置这个属性的话肯定会影响程序的性能!

可以通过模拟器的Debug\Color Blended Layers选项来查看哪些view没有设置为不透明。为了程序的性能,尽可能的将view设置为不透明!