0

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?

TOP KEK
  • 2,593
  • 5
  • 36
  • 62

3 Answers3

3

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.

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Is there anything similair to C# this.Refresh() to repaint the screen ? – TOP KEK Jul 07 '12 at 16:08
  • Actually, alpha is handled immediately by the backing CALayer, and the view is redrawn using drawRect. No need for run loops and timers. – CodaFi Jul 07 '12 at 16:15
  • ctrahey was right. I've modified my code and thrown out the loop. now it works. but I still don't understand why I should break the loop to handle alpha changes – TOP KEK Jul 07 '12 at 16:19
  • 1
    @CodaFi, I just did some testing to verify; but if you are running code like this on the main thread, no updates to the UI happen until you return, and in this case drawRect is not even called at that point(which makes sense, since alpha changes relate to composition and not the actual pixel data of your view). – Chris Trahey Jul 07 '12 at 16:30
2

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;
}];
stevekohls
  • 2,214
  • 23
  • 29
1

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).

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • if you change [[UIScreen mainScreen]brightness] they ban your app in appstore – TOP KEK Jul 07 '12 at 15:59
  • @Ask are you sure? This looks documented and supported to me. – Jesse Rusak Jul 07 '12 at 16:01
  • pretty sure. also there is a bug with restoring brightness level on app close https://discussions.apple.com/thread/3907286?start=0&tstart=0 – TOP KEK Jul 07 '12 at 16:10
  • 1
    Nope. It's supported. Got 2 apps that use it in the store. – CodaFi Jul 07 '12 at 16:11
  • Did you encounter the problem with restoring brightness level on user press Home button? – TOP KEK Jul 07 '12 at 16:13
  • No, it's easy. See [here](http://stackoverflow.com/questions/7900895/uiscreen-brightness-property) for an SO question that "fixes" it. – CodaFi Jul 07 '12 at 16:16
  • My problem was that the whole device brightness was changing on app close and trying to restore it in applicationWillResignActive did not work – TOP KEK Jul 07 '12 at 16:21
  • You need a brightness restore in -applicationDidBecomeActive. – CodaFi Jul 07 '12 at 16:31
  • applicationDidBecomeActive allows you to restore brightness of your app.To restore system screen brightness on app close they suggest to use applicationWillResignActive event which does not work – TOP KEK Jul 07 '12 at 16:33
  • Have you tried performing it in the background in applicationDidEnterBackground? – CodaFi Jul 07 '12 at 16:37