2

I have been looking around for an answer and kind of a "best practise" for these cases as they must happen often for any developer trying to deal with UI and obviously UIKit.

I have read through this thread and numerous other websites without finding a good answer

What I am asking is what the differences in performance are when choosing to hide a view without the necessity to animate them (I am aware of the differences in regards to the visual effect of both properties when they change)

From a pragmatical standpoint I feel that it would be smarter to just set the hidden property and not care about alpha as long as you do not want to animate anything at all. On the other hand, if there is absolutely no difference in performance, why add the property to the UIView class?

In some comments and questions people were saying that the hidden property is true when the alpha value was 0.0f but after some debugging I found out that this is actually not the case, they seem to be completely separate and not connected in any logical way

EDIT

I do want to emphasise that I understand that the difference in performance won't be much, but I still would like to know it and it is the question that is asked. It is not about the usage. This has been taken care of on numerous pages and as well on stackoverflow.

Community
  • 1
  • 1
Benzy
  • 855
  • 7
  • 16

3 Answers3

2

Although I can't speak to a performance difference (if there is one it's bound to be very small) I want to address your final point.

On the other hand, if there is absolutely no difference in performance, why add the property to the UIView class?

If they are treated the same then it could just be to make the code clearer. If you want to hide a view you would expect there to be a 'hidden' property and when reading through code (or searching a large code base to find hidden views) you would be on the lookout for a hidden property naturally, not an alpha property.

  • fair enough, I agree that people should be using hidden but the sad truth is that most developers don't do that. I just recently started using `hidden` over `alpha` to hide views because I simply never knew it. looking at the doc it was available since iOS 2.0 so why aren't we using it? – Benzy Apr 28 '15 at 13:48
1

Unlike hidden, alpha is an animatable property. Basically, that means that you are not setting a value directly, instead when calling setAlpha: an animation is added to the view that handles the alpha change.

This is one of the reasons why things like the following don't always work:

CGFloat alpha = 0.5f;
[view setAlpha:alpha];
CGFloat alpha2 = view.alpha; //not 0.5f!

That means that setting hidden to NO is always preferred over setting alpha to 0.0. Although the difference in perfomance won't be noticeable, setting alpha is still much more complex than using just hidden.

If you want to hide views, use hidden, if you want to change transparency (or animate it), use alpha.

EDIT:

The preference to use alpha over hidden by many programmers can be connected with the fact that usually you are showing and hiding views with an animation. Using only alpha can simplify the code because you are not forced to set two variables at once:

//showing a view
view.hidden = NO;
view.alpha = 0.0f;

[UIView animateWithDuration:0.3 animations: ^{
   view.alpha = 1.0f;
}];

//hiding a view
[UIView animateWithDuration:0.3 animations: ^{
   view.alpha = 0.0f;
} completion:^(BOOL finished) {
   view.alpha = 1.0f;
   view.hidden = YES;
}];

If you ignore hidden and use only alpha to hide views, you can simplify the code above.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • not sure if that actually answers the question, I know about this but was wondering about performance and "best practise" between the two for just hiding a view – Benzy Apr 28 '15 at 13:46
  • @Benzy The difference in perfomance won't be noticeable. Even with `alpha` the animation will be immediate (if you don't set the duration) and when the animation is finished, there will be no difference because the layer won't be displayed at all. The best practice can be summarized by the last sentence in my answer. Just use the simplest solution. – Sulthan Apr 28 '15 at 13:59
  • I do know that the performance in a single execution would not be noticeable. It is still the question that I asked. Thanks for your help! – Benzy Apr 28 '15 at 15:08
0

Just to note, than on macOS, (alpha == 0) != (hidden == YES), so there could be some historical reasons...

Also alpha is float, which can be problematic, as there is only one state (0) that indicates hidden, but so many states indicating visible (especially with animations, maybe it would be error prone if the value would stuck on 0.000001), however hidden is clear

The best approach is to create a UIView subclass, which sets hidden = YES on alpha == 0

Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179