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没有动画效果。

 

1
2
3
4
5
6
7
8
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];

 

设置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,产生的后果是不可预料的~

 

1
2
3
4
5
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.

 

如果了解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.

本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/5149765.html,如需转载请自行联系原作者

相关文章
|
26天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
3月前
|
安全 Android开发 数据安全/隐私保护
请说明鸿蒙操作系统与其他操作系统(如Android和iOS)的主要区别。
请说明鸿蒙操作系统与其他操作系统(如Android和iOS)的主要区别。
58 1
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
2月前
|
Java 测试技术 API
安卓APP和iOS APP在测试上的区别是什么?
安卓APP和iOS APP在测试上的区别是什么?
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
26 1
|
3月前
|
存储 安全 数据安全/隐私保护
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
83 4
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
3月前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
42 2