11

Why does the UISlider view ignore the alpha view when set to 0.5?

Code:

for (int i = 0; i < 3; i++) {
  UISlider *slider = [[[UISlider alloc]
                       initWithFrame:CGRectMake(0, i * 30, 200, 30)]
                      autorelease];
  slider.alpha = 0.4 + (CGFloat)i / 10.0f;
  [window addSubview:slider];
}

Result:

Example screenshot

The sliders have alpha values 0.4, 0.5 and 0.6. And as you can see the middle one with 0.5 is completely opaque. It seams to only occur with alpha 0.5. Have tested other UI controllers and they work as expected with alpha is set to 0.5.

Reproduced with iOS 4.2 on real device and with iOS 3.2 and 4.2 in simulator.

BTW if someone curious how and why I hit this problem it's the sliding direction pad configuration for a puzzle game called Slippy.

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
  • 4
    Spending a couple minutes playing around with it myself, it sure looks/feels like a bug. I would suggest filing a bug report with Apple - http://developer.apple.com/bugreporter/ – Eric Jan 13 '11 at 17:37
  • Have reported it to apple now. Will update when i know more. – Mattias Wadman Jan 13 '11 at 17:48
  • 1
    Try different starting values: `0.41 + ...`, `0.3 + ...`. This might yield some insight into what exactly is wrong (is it the second item, the value 0.5, ...?) Also, try adding `if (i != 1) continue` as the first line of the `if` statement, to see if the other sliders are interfering with the middle one. One more thing comes to mind on the "interference" angle: separate them by 100 pixels instead of 30. – Marcelo Cantos Feb 07 '11 at 04:38
  • Also, remove the `autorelease` (this might be a dumb suggestion, but since you're clutching at straws by now, this is a fairly easy "straw" to test). – Marcelo Cantos Feb 07 '11 at 04:44
  • Thanks for the tips but none of them seams to work. It's the same with just one slider set to alpha 0.5 and it seams to only happen for exactly 0.5. I currently use a ugly workaround that is not noticeable visually (alpha == 0.5f ? 0.49f : alpha) – Mattias Wadman Feb 07 '11 at 10:12
  • 1
    Yep, that's definitely a bug in `UISlider`. – Dave DeLong Feb 07 '11 at 22:28
  • @david The bug has problem ID 8859765 at bugreport.apple.com. – Mattias Wadman Feb 07 '11 at 23:35
  • @Mattias Thanks; I've started watching it. For now, the best work around is to use a value that's close to, but not quite, 0.5. From what I can tell, anything that's not *exactly* 0.5 should work. – Dave DeLong Feb 08 '11 at 00:49
  • Good. Yes that's what im doing right now and it works fine. – Mattias Wadman Feb 08 '11 at 02:23
  • Got notification that this was fixed in iOS 5.0 beta 1 a while ago but haven't been able to update to 5.0 yet. – Mattias Wadman Aug 07 '11 at 15:02

1 Answers1

1

As you said that other UI controllers work with 0.5 alpha, there should be no difference with UISlider, since they inherit alpha property from UIView class and there is reference to the opaque property ("You should always set the value of this property to NO if the view is fully or partially transparent"). Maybe you can try to follow the advice.

If there's really a bug with 0.5 value, you can simply change your starting transparency from 0.4 to 0.41/0.39 w/o any visible difference:

slider.alpha = 0.41f + (CGFloat)i / 10.0f;

Finally, you can output the resulting alpha values to some labels to check if they are the expected ones or output the (CGFloat)i value to see if something wrong with type casting.

Ilya Dvorovoy
  • 414
  • 4
  • 6
  • slider.opaque = NO does not seam to work either. I also logged the alpha value with NSLog(@"%f", slider.alpha) and it prints 0.4, 0.5 and 0.6. In the application where is hit this bug i only have one slider, the loop was just to illustrate the problem. Thanks for the suggestions. – Mattias Wadman Feb 07 '11 at 21:46