Does iOS send notifications when the system changes the screen brightness?

老子叫甜甜 提交于 2019-12-01 03:52:54

问题


Problem is, my reading app has a button that puts it into dark theme mode. The brightness gets reduced by 10%. When the user returns to the normal mode, it is set back to the remembered brightness. But in the meantime the actual system brightness could have changed due to auto-brightness adjustment or even the user going to Settings and changing it there.

The problem with the brightness property is that you only query actual screen brightness and whatever you set is only temporarily until the user locks the screen. upon unlock the system reverts to system brightness.

If iOS would send notifications when the system changes the brightness that would be helpful. Couldn't find anything on this in the docs.


回答1:


Search for UIScreenBrightnessDidChangeNotification which is part of the UIScreen class. It is available from iOS 5.0 and above.




回答2:


If you want to set the brightness back to a programatic value when the device is unlocked again, observe the UIApplicationDidBecomeActiveNotification notification and set the brightness back to you desired level in your selector.

I've done some more research on this whole brightness thing and here's what I found:

The UIScreenBrightnessDidChangeNotification gets called ONLY when the system changes the brightness or when the user changes the brightness from the control panel or settings. NOT when you change the brightness programatically.

What I assumed from this Apple Doc here (see below) is that brightness will not change anymore after you set it programatically.

Brightness changes made by an app remain in effect until the device is locked, regardless of whether the app is closed. The system brightness (which the user can set in Settings or Control Center) is restored the next time the display is turned on.

However I found this not to be true (or I'm misinterpreting it). The brightness does change (and UIScreenBrightnessDidChangeNotification does get called) after you set the brightness programatically. However it ONLY gets called when the system thinks it should increase or decrease the brightness (due to changes in the environment brightness) *. If the brightness of the environment stays the same your screen will stay as bright as you set it programatically.

What does this mean? Well, 2 things:

  • If you want to keep the brightness at the level you set it programatically, no matter environment changes, you have to observe the UIScreenBrightnessDidChangeNotification and reset the brightness to your desired level every time it's called.
  • If you want to go back to the "system" brightness, well you can't really, because you simply can't tell what the system brightness would be if environment changes took place (because you are resetting it every time). You have 2 options for this.
    1. Remember the brightness before you set your brightness programatically and set it back to that value. Or
    2. Put the brightness to 0.5 and let the system to it's job.

The 'danger' in both situations is that it will stay on the value you set it until an environment brightness change is detected.

*There are 2 special cases... when you set the brightness to 0.0 and the system thinks it should decrease brightness nothing happens because it's already on 0.0. Secondly, when you set it to 1.0 it will stay on 1.0, no matter how the environment changes.



来源:https://stackoverflow.com/questions/14719443/does-ios-send-notifications-when-the-system-changes-the-screen-brightness

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!