关于UIView的三个容易混淆的属性,alpha,opaque,hide的简要说明

关于UIView的三个容易混淆的属性,alpha,opaque,hide的简要说明。

1)首先贴出这3个属性在文档里的解释
?
1
@property(nonatomic)                 CGFloat           alpha;                      // animatable. default is 1.0


The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. This value affects only the current view and does not affect any of its embedded subviews.
Changes to this property can be animated.
alpha的值是一个在0.01.0间的浮点值,当alpha为0.0时表现为完全透明,为1.0时表现为完全不透明。如果设置了一个UIView的alpha后,仅仅会影响你所设置的视图,而不会影响身处其中的子视图。

经过实际操作知道,当设置了子视图的alpha后,父视图的显示不受影响并其alpha不变;当设置了父视图的alpha后,子视图的显示将会有影响(朦胧的感觉),但子视图的alpha不变


?
1
@property(nonatomic,getter=isHidden) BOOL              hidden;                     // default is NO. doesn't check superviews


Setting the value of this property to YES hides the receiver and setting it to NO shows the receiver. The default value is NO.
如果将某UIView的该属性设置为YES,将影藏该VIEW;设置为NO,显示该View,默认为NO。

A hidden view disappears from its window and does not receive input events. It remains in its superview’s list of subviews, however, and participates in autoresizing as usual. Hiding a view with subviews has the effect of hiding those subviews and any view descendants they might have. This effect is implicit and does not alter the hidden state of the receiver’s descendants.
一个已经将其hidden设置为YES的视图将会从它所属于的窗口上消失,并不在接受输入事件。但其依然保留在其父视图的子视图列表里,并且照旧执行自动布局。影藏一个包含子视图的视图将会对其子视图有影响并影藏它们,这种影响或作用是明确明了的,但是其不会改变其子视图的hidden值。

Hiding the view that is the window’s current first responder causes the view’s next valid key view to become the new first responder.
影藏当前窗口为第一响应者的视图,将会导致该视图的下一个有效视图为新的第一响应者。(关于 the view’s next valid key view还需要查阅资料,暂时不明白)

The value of this property reflects the state of the receiver only and does not account for the state of the receiver’s ancestors in the view hierarchy. Thus this property can be NO but the receiver may still be hidden if an ancestor is hidden.
该属性的值反应了该VIew是否处于影藏状态,但是不影响该View父视图们的影藏与否状态。因此可能某子视图的该属性值为NO,但是其依然影藏如果其某个父视图(层级树--view hierarchy)是影藏状态。


换句话说,子视图影藏了,不会影响其父视图;父视图影藏了,会影响其子视图。 设置hidden后的效果就是要么看的见要么看不见。

?
1
@property(nonatomic,getter=isOpaque) BOOL              opaque;                     // default is YES. opaque views must fill their entire bounds or the results are undefined. the active CGContext in drawRect: will not have been cleared and may have non-zeroed pixels

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.
该属性为绘画系统提供了一个线索关于如何刻画视图,如果设置为YES,该绘画系统将会把视图当做全部不透明,并且允许系统最大可能的优化绘画操作已提高性能,如果设置为NO,绘画系统将会复合操作该视图。默认值是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.
一个不透明的视图期待使用完全不透明的内容覆盖其bounds,该content其alpha应该为1.0。如果某视图为不透明但是没有完全的填充其
bounds或部分透明,结果将会无法预期。你应该将该属性设置为NO,如果该View是全部或部分透明。

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.
你仅仅需要设置UIVIEW的子类的opaque属性当你使用 drawRect:方法绘制自己的内容时;对于系统提供的类,设置opaque属性没有效果,比如UIButton, UILabel, UITableViewCell等。

1)虽然翻译了,但是对于opaque属性的 具体作用依然不太清楚,希望有了解的人可以指点一二。
只知道在绘图时关于开销方便会受影响。


查阅资料已经参考某些童鞋们的讲解,现对opaque总结如下:
opaque
该属性为BOOL值,UIView的默认值是YES。opaque表示当前UIView是否不透明,事实上它却决定不了当前UIView是不是可见与不可见。比如你将opaque设为NO,该UIView照样是可见的(上文说过,是否可见是由alpha或hidden属性决定的),照理说为NO就表示透明,那就应该是不可见的呀?
卖个关子,先看下图:

前面讲过,显示器中的每个像素点都可以显示一个由RGBA颜色空间组成的色值,比如上图中有红色和绿色两个图层色块,对于没有交叉的部分,即纯红色和绿色部分来说,对应位置的像素点只需要简单的显示红或绿,对应的RGBA为(1001)和(0101)就行了,负责图形显示的GPU需要很小的计算量就可以确定像素点对应的显示内容。
问题是红色和绿色还有相交的一块,其相交的颜色为黄色。这里的黄色是怎么来的呢?原来,GPU会通过图层一和图层二的颜色进行图层混合,计算出混合部分的颜色,最理想情况的计算公式如下:
R = S + D * ( 1 – Sa )

其中,R表示混合结果的颜色,S是源颜色(位于上层的红色图层一),D是目标颜色(位于下层的绿色图层二),Sa是源颜色的alpha值,即透明度。公式中所有的S和D颜色都假定已经预先乘以了他们的透明度。

知道图层混合的基本原理以后,再回到正题说说opaque属性的作用。当UIView的opaque属性被设为YES以后,按照上面的公式,也就是Sa的值为1,这个时候公式就变成了:

R = S
即不管D为什么,结果都一样。因此GPU将不会做任何的计算合成,不需要考虑它下方的任何东西(因为都被它遮挡住了),而是简单从这个层拷贝。这节省了GPU相当大的工作量。由此看来,opaque属性的真实用处是给绘图系统提供一个性能优化开关!

按照前面的逻辑,当opaque属性被设为YES时,GPU就不会再利用图层颜色合成公式去合成真正的色值。因此,如果opaque被设置成YES,而对应UIView的alpha属性不为1.0的时候,就会有不可预料的情况发生,这一点苹果在官方文档中有明确的说明:
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.
所以你应该将该属性设置为NO,如果该View是全部或部分透明


希望对某些童鞋有帮助。

原文网址:http://www.cocoachina.com/bbs/read.php?tid-282591-page-3.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值