3

I don't want the phone to go to sleep so I am using:

    [ [ UIApplication sharedApplication ] setIdleTimerDisabled:YES ] ;

but I do need the phone to gray out its screen to not waste the battery. I have already seen this question How can I dim the view in an iPhone application?, but need more detail please.

I am trying something like this:

to set the opaqueWindow as frontmost (which has a 320 x 480 black image loaded)

[opaqueWindow makeKeyAndVisible];
[ opaqueWindow setAlpha:1.00 ] ;        
[ mainWindow setAlpha:0.10 ] ;

and to try to go back to the mainWindow and set it frontmost (a picker control and some labels):

    [ opaqueWindow setAlpha:0 ] ;       
    [mainWindow makeKeyAndVisible];
    [ mainWindow setAlpha:1.00 ] ;

There can only be one key window in my app, right? so when mainWindow becomes key, then opaqueWindow isn't and vice versa. I create opaqueWindow and access mainWindow as follows:

    opaqueWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen ] bounds]];
    opaqueView = [[UIView alloc] init];
    [opaqueWindow addSubview:opaqueView];

mainWindow = [ myApplication keyWindow ] ;

Even when I try to make my mainWindow active again, my controls are still grayed out and opaqueWindow still seems to have control.

I am a newbie at iphone development, I have been looking at the Cocoa Touch for iphone 3 developer reference and a few other books.

I would appreciate any help or advice.

thank you!

Piesia

Edit: I'm assuming that opaqueWindow will need to pass down button clicks to mainWindow while opaqueWindow is key. Do I just call mainWindow's button handler from within opaqueWindow's button handler or is there a better way to do it. thanks! P.

Community
  • 1
  • 1
Piesia
  • 185
  • 3
  • 8

2 Answers2

8

As of iOS 5 you can use the public API:

[[UIScreen mainScreen] setBrightness:1.0];//replace 1.0 with any value...

Beats using the opacity frame thing...

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • Thank you, Adam. I appreciate the information on the iOS 5 API, I will need to stay compatible with iOS 4 devices (for right now), however, as there are still a lot of iPhone 3g phones that won't support iOS 5. – Piesia May 12 '12 at 22:56
4

While it might make sense from a usability perspective, I doubt that overlaying the window with a gray view actually improves battery run time. You would have to dim the backlight, which is the main energy drain. This is only possible with a jailbroken phone or maybe a private API (which will prevent your app from being approved by Apple).

Use this method in your view controller to dim by adding a black view with 50% alpha. Make sure to set userInteractionEnabled = NO to pass events to underlying views.

- (IBAction)dim:(id)sender {
    UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    dimView.backgroundColor = [UIColor blackColor];
    dimView.alpha = 0.5f;
    dimView.userInteractionEnabled = NO;
    [self.view addSubview:dimView];
}

In case you want to push your luck and risk being rejected, try this private API call:

[(id)[UIApplication sharedApplication] setBacklightLevel:1.0f];
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • Now I can't find where I saw the description, but it was something like this: I'm able to get the screen to 'dim' by bringing the black image to the front with make KeyAndVisible, but I can never then bring my mainWindow to the front again and user clicks go nowhere. The private function looks good, but I need to have appstore acceptability for the app. BTW, I tried setBacklightLevel:0.1f and got a compile time warning warning: 'UIApplication' may not respond to '-setBacklightLevel:'". The app also didn't dim. thanks – Piesia Dec 16 '09 at 19:51
  • sorry, daniel. I ran out of space. Thanks very much for your suggestions. Do you have any suggestions as how I can bring my non-opaque window (mainWindow) back to the front and accepting user input? Can you recommend the proper way to transfer button click and other events from opaqueWindow to mainWindow? Some kind of responder chain (if that's the term for it) or just calling mainWindow's button handler from opaqueWindow's? Thanks for your patience! Piesia – Piesia Dec 16 '09 at 19:54
  • I edited my answer, I hope the method does what you are looking for. – Daniel Hepper Dec 16 '09 at 22:47
  • Thanks very much for all your help, I'll give it a try, it looks like just the thing for what I need. (I will give you a vote up as soon as I can). I really appreciate your time and advice! best regards, Piesia – Piesia Dec 17 '09 at 03:16
  • Thanks, Daniel, it works perfectly! I added (to viewDidLoad];): dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];; dimView.backgroundColor = [UIColor blackColor]; dimView.alpha = 0.0f; dimView.userInteractionEnabled = NO; [ self.view addSubview:dimView ]; in viewDidUnload I put: [ dimView release ] then when I needed to dim my screen I just issued: dimView.alpha = 0.5f; when I didn't want the screen dimmed, I restored it with: dimView.alpha = 0.0f; Whether it'll help save the battery, I don't know, but it looks sharp! – Piesia Dec 17 '09 at 04:22