简体   繁体   English

iOS5 setBrightness不适用于applicationWillResignActive

[英]IOS5 setBrightness didn't work with applicationWillResignActive

I use [[UIScreen mainScreen]setBrightness: ] (in sdk 5.0) to change the system background light in my app. 我使用[[UIScreen mainScreen]setBrightness: ] (在sdk 5.0中)更改了我的应用程序中的系统背景光。

The following steps work with my app 以下步骤适用于我的应用

  1. Active the app, get the system brightness as default, then save as sysBright . 激活应用程序,获取默认的系统亮度,然后另存为sysBright

  2. Change the brightness with my app, changed brightness, then save as appBright . 使用我的应用程序更改亮度,更改亮度,然后另存为appBright

  3. ResignActive app with home button or lock button, set brightness to sysBright (step 1 value, system default brightness). 使用主屏幕按钮或锁定按钮的ResignActive应用程序,将亮度设置为sysBright (步骤1的值,系统默认亮度)。

  4. Active app again. 再次启用应用。 Then it will repeat the above steps form 1 to 3. 然后将重复上述步骤1至3。

Something is wrong with step 3, when I inactivate the app with the lock button, the function applicationWillResignActive works well, it can restore the brightness value ( sysBright ). 步骤3出了点问题,当我用锁定按钮将应用程序停用时,函数applicationWillResignActive可以正常工作,它可以恢复亮度值( sysBright )。

But when I press the home button, it doesn't work anymore. 但是当我按下主页按钮时,它不再起作用。 The brightness is still the value I changed in my app. 亮度仍然是我在应用程序中更改的值。 ( appBright ) appBright

Does anyone have any idea about it? 有人对此有想法吗? Thanks for any help ~ 谢谢你的帮助〜

Here is the code: 这是代码:

float appBright,sysBright;

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    sysBright = [[UIScreen mainScreen] brightness];
    [[NSUserDefaults standardUserDefaults] setFloat:sysBright forKey:@"sysBright"];

    [[UIScreen mainScreen] setBrightness:appBright];
}

//doesn't work when i ResignActive with the home button
- (void)applicationWillResignActive:(UIApplication *)application
{        
    [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:sysBright];        
}

iOS is not meant to retain in-app brightness values. iOS并非要保留应用内亮度值。 It should restore system value after the app resigns active, quits, crashes etc. So officially there is no need to do that in applicationWillResignActive. 在应用程序退出活动,退出,崩溃等之后,它应该恢复系统值。因此,正式而言,无需在applicationWillResignActive中执行该操作。

But it does't work. 但这不起作用。 It's a bug. 这是一个错误。 In fact it works if you switch to another app (press home button twice and select another app) 实际上,如果您切换到另一个应用程序(两次按主屏幕按钮并选择另一个应用程序),则可以使用

Don't waste your time just file a bug report to Apple (I did well). 不要浪费时间,只需向Apple提交错误报告(我做的很好)。

Unlock screen restores default system brightness. 解锁屏幕可恢复默认的系统亮度。 Just press the power button twice and unlock to restore original brightness. 只需按两次电源按钮并解锁即可恢复原始亮度。

Try this... 尝试这个...

- (void)applicationWillResignActive:(UIApplication *)application
{        
    CGFloat brightness = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:brightness];        
}

There are cases when you really need to suspend the application (make it go to background, like when you push the Home button) and still preserve the brightness you have previously set on the screen. 在某些情况下,您确实需要挂起该应用程序(使其进入后台,例如按“主页”按钮时),并且仍然保留您先前在屏幕上设置的亮度。

Example: I 'm currently working on an underwater application (it takes photos with an iPhone in an waterproof case for scientific reasons) and we don't have access to the whole device screen. 示例:我目前正在水下应用程序中(出于科学原因,它在防水的情况下使用iPhone拍摄照片),但我们无法访问整个设备的屏幕。

The underwater housing implements 3 mechanical "touch" buttons on very specific places and we have to disable the auto-lock feature because there is no way to perform a slide-gesture to unlock the device. 水下外壳在非常特定的位置实现了3个机械“触摸”按钮,我们必须禁用自动锁定功能,因为无法执行滑动手势来解锁设备。

We still need a way to preserve battery life when not using the application ie suspend the application and setting a low level on screen brightness. 当不使用应用程序时,我们仍然需要一种方法来保存电池寿命,即挂起应用程序并设置较低的屏幕亮度。

The solution we implemented is: 我们实施的解决方案是:

a) We tell the user to turn the Auto-Brightness off in settings and disable the auto-lock feature a)我们告诉用户关闭设置中的自动亮度并禁用自动锁定功能

b) We turn the Brightness to 100% on ApplicationDelegate class: b)我们将ApplicationDelegate类的亮度设置为100%:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Adjust Brightness to full
    [[UIScreen mainScreen] setBrightness:1.0];
}

c) We execute the following code with the touch on an application button, to ensure that the application goes to background (not consuming CPU cycles) and the brightness go to lower level possible (preserve battery): c)我们通过触摸应用程序按钮来执行以下代码,以确保应用程序进入后台(不消耗CPU周期)并且亮度降低到可能的较低水平(节省电池):

[[UIScreen mainScreen] setBrightness:0.0]; // Set to low brightness
[[UIApplication sharedApplication] performSelector:@selector(suspend)]; // Simulate Home button

I hope this could be helpful to somebody. 我希望这会对某人有所帮助。

PS: The Apple Human interface guides is one thing and the actual needs of a real-world application is another (you cannot predict or restrict anything in advance). PS:Apple Human界面指南是一回事,而实际应用程序的实际需求是另一回事(您无法预先预测或限制任何事情)。

根据Apple的DevForum的说法,这似乎是Apple不愿意修复的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM