Reputation: 2651
I'm trying to create an UIView and change its alpha property to simulate backlight change. Here is my code
TransparentView = [[UIView alloc] initWithFrame:self.view.bounds];
TransparentView.backgroundColor = [UIColor whiteColor];
self.view = TransparentView;
TransparentView.alpha = 0.2;
float step = 1.0 / ( appDelegate.OnOffTime * 100);
for (float f = 0; f < 1; f=f+step) {
TransparentView.alpha = (CGFloat)(1 - f);
[NSThread sleepForTimeInterval:0.01];
}
Both TransparentView.alpha = 0.2
and TransparentView.alpha = (CGFloat)(1 - f)
do change TransparentView.alpha
, but only TransparentView.alpha = 0.2
changes real device "brightness'.
What am I doing wrong?
Upvotes: 0
Views: 4099
Reputation: 2255
You may want to consider changing your code to use the built-in UIView animation support. This will ensure that your view is updated and you aren't sleeping the main thread. If you need to do something special once the animation completes, you can use the completion: version of the method.
TransparentView.alpha = 0.2;
[UIView animateWithDuration: appDelegate.OnOffTime animations:^{
TransparentView.alpha = 0.0;
}];
Upvotes: 2
Reputation: 18290
I think the issue is that you are sleeping the thread, and not actually setting up an asynchronous timer. What this means is, you actually are not letting the run loop get around to making your changes on screen, because you are not exiting this loop until it's back up to 1 :-)
EDIT: to clarify:
Any change you make to your UI will not actually get drawn until the run loop "comes back around", which requires your method to return. You likely want an NSTimer instance, configured to call a method on this object which increments the alpha (and checks the alpha against your destination value; invalidating the timer when it reaches it.
Upvotes: 3
Reputation: 43330
Alpha and luminance are two different concepts for a view. If you really wanted to change the luminance of the display, you can use iOS 5+'s [[UIScreen mainScreen]brightness];
, otherwise your alpha changes need to be against a black background and not using such strange numbers as a cast 1-float. My bet is that your little algorithm is returning a large enough step factor that your view is being told to display alpha values over one (which defaults to one anyhow).
Upvotes: 1