iOS:Alpha、Hidden 与 Opaque 区别

UIView中的这三个属性用的比较多,尤其是 Alpha 和 Opaque 之间有的时候不是很好分别:

Alpha(不透明度)

alpha 是不透明度,属性为浮点类型的值,取值范围从 0 到 1.0,表示从完全透明到完全不透明,其特性有当前 UIView 的 alpha 值会被其所有 subview 继承。alpha 值会影响到 UIView 跟其所有 subview,alpha 具有动画效果。当 alpha 为 0 时,跟 hidden 为 YES 时效果一样,但是 alpha 主要用于实现隐藏的动画效果,在动画块中将 hidden 设置为 YES 没有动画效果。

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 100, CGRectGetWidth(self.view.bounds)-60, 150)];
[view setBackgroundColor:[UIColor redColor]];
[view setAlpha:0.5];
[self.view addSubview:view];
 
UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(20, 30, 100, 80)];
[childView setBackgroundColor:[UIColor blueColor]];
[view addSubview:childView];
 
66560-50c954e033eb6aec.png
效果图

设置 backgroundColor 的 alpha 值只影响当前 UIView 的背景,并不会影响其所有 subview。Clear Color 就是backgroundColor 的 alpha 为1.0。alpha值会影响backgroundColor 最终的 alpha,假设 UIView 的 alpha 为0.8,backgroundColor 的 alpha 为0.5,那么backgroundColor 最终的 alpha 为 0.4(0.8*0.5)。

Hidden(隐藏)

Hidden 表示 UIView 是否隐藏,Hidden 设置为 YES 表示当前 UIView 的所有 subview 也会被隐藏,忽略 subview 的 hidden 属性。Hidden 只要设置为 YES,所有的 subview 都会隐藏。UIView 隐藏之后也会从当前的响应者事件中移除。

Opaque

opaque 也是表示当前的 UIView 的不透明度,设置是否之后对于 UIView 的显示并没有什么影响,官方文档的意思简单点说就是 opaque 默认为 YES,如果 alpha 小于 1,那么应该设置 opaque 设置为NO,但是如果 alpha为1,opaque 设置为 NO,产生的后果是不可预料的。

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 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.

翻译:

这个属性向绘图系统提供了一个提示,说明它应该如何处理视图。如果设置为YES,绘图系统将视图视为完全不透明的,这允许绘图系统优化一些绘图操作并提高性能。如果设置为NO,绘图系统通常会将视图与其他内容组合在一起。此属性的默认值为YES。
一个不透明的视图应该用完全不透明的内容来填充其边界――也就是说,内容的alpha值应该为1.0。如果视图是不透明的,或者没有填充其边界,或者包含全部或部分透明的内容,那么结果是不可预测的。如果视图是完全透明或部分透明的,您应该始终将此属性的值设置为NO。
您只需要为UIView的子类设置一个不透明属性的值,这些子类使用drawRect:方法绘制它们自己的内容。不透明属性对系统提供的类(如UIButton、UILabel、UITableViewCell等)没有影响。

如果了解 opaque,需要点屏幕绘制的知识,屏幕上的每个像素点都是通过 RGBA 值(Red、Green、Blue 三原色再配上 Alpha 透明度)表示的,当纹理(UIView 在绘图系统中对应的表示项)出现重叠时,GPU 会按照 Result = Source + Destination * (1 - SourceAlpha)公式计算重叠部分的像素。

Result 是结果 RGB 值,Source 为处在重叠顶部纹理的 RGB值,Destination 为处在重叠底部纹理的 RGB 值。

当 SourceAlpha 为 1 时,绘图系统认为下面的颜色全部被遮盖住了,Result = Source,如果 Source 的 Alpha 不为 0,上下层颜色就会进行合成,所以 opaque 默认设置 YES,提升绘制性能,如果开发中 UIView 是不透明的,opaque 设置为YES, 如果 opaque 设置NO,那么Alpha应该小于1。

 
66560-52b77abc6e8493b1.png
效果图
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值