25

I want to use animation to change UIView's color to make it fade, however, somehow i can't access and set opacity, but i can set alpha. I wonder if alpha and opacity are the same? Search on the web and I didnt find a good answer. Thanks a lot for your help!

trillions
  • 3,669
  • 10
  • 40
  • 59

3 Answers3

40

Actually "opacity" means "value of alpha-channel" of your UIView. When a view is fully opaque this means its alpha = 1, when a view is fully transparent (non-opaque) its alpha = 0.

As about properties of CALayer and UIView in Cocoa, yes, they provide the same functionality.

John Topley
  • 113,588
  • 46
  • 195
  • 237
alex
  • 2,121
  • 1
  • 18
  • 24
  • Thanks Alex! And I realized to access alpha is easier than access opacity in coding, since the latter one requires import the QuartzCore lib. – trillions Mar 13 '13 at 19:17
  • 5
    What is the preferred way? `view.alpha` vs `view.layer.opacity`? – Anoop Nyati Sep 05 '18 at 08:50
  • If both things are same and one is easier to set. You can choose which one you prefer. For me Alpha is preferred way. – AZ_ Aug 06 '20 at 13:48
13

Just try by yourself!

CALayer has opacity from 0 to 1, while UIView has alpha from 0 to 1. They have the same visual effect.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • 3
    They do have same visual effect, however is there any performance impact in using either of the way of coding? – Anoop Nyati Sep 05 '18 at 08:49
8

Alpha affects everything drawn on the view.The background color's alpha affects the background color's transparency and anything else drawn on the view.i.e it brings about a frosty effect to whole view. Opacity means don't draw anything underneath, even if you are transparent, it just effects the current view.

Jens
  • 6,173
  • 2
  • 24
  • 43
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
  • It doesn't impact subviews, so not quite 'everything drawn on the view' - from Apple's docs: *"This value affects only the current view and does not affect any of its embedded subviews"* – lxt Mar 13 '13 at 09:58
  • I think there is another confusion...There is a field called "BOOL Opaque" which has some description about how it will affect subviews... – trillions Mar 13 '13 at 19:18