• 背景色默认值引出的一系列问题


    刚看到代码中有大量的xx.backgroundColor = [UIColor clearColor];

    (1)很好奇,默认的背景色是什么值?

    官方给出的:默认为nil,此时背景是透明的。然后clearColor的备注也写明了:“0.0 white, 0.0 alpha”(白色,但alpha为0),所以我感觉代码中设置clearColor的代码可以去掉,除非此举是为了reset背景色。

    (2)结束了这个问题后,想到了UIView有个属性是opaque,如果此属性设置为YES,也就是不透明,但是默认背景色是透明的,那。。。。会发生什么?

    opaque属性默认为YES,这点自己没有理解透彻,如果是YES,那为什么叠加的View会完全被看到呢?跟背景色的默认值是不是冲突了呢?翻看官方文档后发现:

    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.

    opaque为YES的视图的内容区域必须不透明:内容的alpha值需要为1。如果设置了opaque属性为YES,但内容中有部分透明或者全部透明,结果会是无法预期的。这样最好还是要设置为NO。

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

    opaque属性只需要对继承自UIView的、使用drawRect:进行绘制内容的类(自定义类)中使用,对系统提供的类,例如UIButtonUILabelUITableViewCell,等,使用此属性,是没有效果的。

    这下明白了,这个属性更多是为drawRect:方法绘制出内容的对象准备的,如果更多的是对系统提供控件的使用,一般情况下我们用不到。

     

    然后我又看到了一篇相关文章,顺便延伸一下:

    原文地址:http://blog.csdn.net/wzzvictory/article/details/10076323

    文章讲述了hidden、alpha、opaque的区别,其中说到了:

    alpha为0和hidden为YES时,当前UIView会从响应者链中移除,而响应者链中的下一个会成为第一响应者,这一点比较有用

    作者还提到了,alpha设置为0和hidden为YES有什么区别,我知道的是,在Windows Phone开发中,前者是需要CPU或GPU绘制的,而后者是不需要的,在iOS上是否如此,也希望大家不吝赐教!

     

     

  • 相关阅读:
    在Linux系统中Navicat for MySQL 出现1045错误如何处理
    一个老程序员这些年的心得体会
    忘了
    DAY11
    day10_plus
    day10
    东北育才冲刺noip(day9)
    Java语言Socket接口用法详解
    JDBC-ODBC桥连接方式操纵SQL数据库
    JDBC连接SQL Server 2005步骤详解
  • 原文地址:https://www.cnblogs.com/lihaiyin/p/4736012.html
Copyright © 2020-2023  润新知