UIView alpha与UIColor alpha [英] UIView alpha vs. UIColor alpha
问题描述
我想知道它们之间的区别:
I would like to know the difference between:
- 为我的UIView分配一个颜色,其中< 1 alpha vs
- 为它指定一个不透明的颜色,但给UIView一个< 1 alpha值。
在屏幕截图中,我制作了两个UIViews,每个UIVabel上有两个黑色(alpha = 1.0)UILabel:
On the screenshot I have made two UIViews with two black (alpha = 1.0) UILabels on top of each:
假设之前定义了一个宏_RGB:
Assume a macro _RGB is defined before:
#define _RGB(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
然后这里是代码::
[_view1 setBackgroundColor:_RGB(255, 0, 0, 1)];
[_view1 setAlpha:0.5];
[_view2 setBackgroundColor:_RGB(255, 0, 0, 0.5)];
[_view2 setAlpha:1];
[_view3 setBackgroundColor:_RGB(255, 0, 0, 1)];
[_view3 setAlpha:1];
我只能看到一个视觉差异:更改视图自己的alpha而不是bg颜色,会影响子视图好。但除此之外,我应该考虑功能上的任何差异吗? (例如,关于动画,图层等)
I can see only one difference visually: Changing view's own alpha instead of bg color's, affects the subviews as well. But other than that is there any difference in functionality that I should consider? (eg. On animations, layers, etc.)
推荐答案
区别在于:
- 通过设置背景颜色(alpha< 1),只有背景颜色会有alpha,所有子视图默认都有alpha 1,即100%不透明
- 通过视图的设置alpha,将使用给定的alpha绘制整个视图(包括所有子视图)
所以在你的例子中:
-
_view1
(alpha< 1 on视图):alpha也应用于子视图UILabel -
_view2
(alpha< 1 on background color):subview UILabel很好opaque -
_view3
(两者都是defualt alpha 1):好吧,我们都看到:-)背景颜色和标签都是不透明的
_view1
(alpha <1 on view): alpha is also applied to the subview UILabel_view2
(alpha <1 on background color): subview UILabel is nicely opaque_view3
(defualt alpha 1 on both): well, we all see :-) background color and label are both opaque
两者的示例用法:
左图:你有一个半透明的黑色叠加层,其子视图 UIImageView
包含一个白色的复选标记图标,您通常希望图标完全不透明(不透明)。所以你只设置背景颜色为黑色,alpha <1(在示例中为0.75f)。
Left image: You have a semi-transparent black overlay which has a subview UIImageView
containing a white checkmark icon and you usually want the icon to be fully opaque (non-transparent). So you set just the background color black with alpha <1 (0.75f in the example).
反对:
右图:你有一个半透明的黑色叠加层,它有一个包含白色图案图像的子视图 UIImageView
是不透明的白色,没有alpha)。并且您希望叠加中的图案也是半透明的。然后你将背景颜色设置为黑色,alpha 1(不透明)和alpha< 1到整个视图。
Right image: You have have a semi-transparent black overlay which has a subview UIImageView
containing a white pattern image (the pattern is opaque white, no alpha). And you want the pattern in overlay to be also semi-transparent. You then would set background color to black with alpha 1 (opaque) and alpha <1 to the whole view.
如你所见,左图上的图标是完全的不透明,但在右边,你也可以看到星星图案。
As you can see, icon on left image is fully opaque, but on a right, you can see also through a stars pattern.
这篇关于UIView alpha与UIColor alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!