ASCScreenBrightnessDetector
ASCScreenBrightnessDetector lets you easily detect screen brightness changes and provides some useful delegate methods.
For Example it's very easy to switch between a day and night theme optimized for different lighting conditions:
ASCScreenBrightnessDetector能让你非常便利的检测屏幕亮度,并提供实用的代理方法供你使用。
例如,下面的例子中,在白天和黑夜之间切换可以适用不同的主题。
Usage
This repository contains an example project that uses the methods provided by ASCScreenBrightnessDetector - just build and run to see it in action.
这个例子中已经包含了使用样例-你可以编译然后自己看一下效果。
Please note: The screen brightness detection will only work on a real device, the Xcode Simulators screen brightness is always 0.5.
注意:屏幕亮度检测只会在真实的设备上使用,模拟器上面永远都是0.5.
Wherever you want to use ASCScreenBrightnessDetector, import the header file as follows:
单例想用ASCScreenBrightnessDetector的时候,导入头文件:
#import "ASCScreenBrightnessDetector.h"
or when using CocoaPods:
如果你用的是CocoaPods,就这么导入头文件:
#import <ASCScreenBrightnessDetector/ASCScreenBrightnessDetector.h>
To detect the current screen brightness or style you can easily use:
为了检测当前屏幕亮度,你可以这样子使用:
ASCScreenBrightnessDetector *brightnessDetector = [ASCScreenBrightnessDetector new]; NSLog(@"Screen brightness: %f", brightnessDetector.screenBrightness); ASCScreenBrightnessStyle style = brightnessDetector.screenBrightnessStyle; switch (style) { case ASCScreenBrightnessStyleDark: // Do something, e.g. set a dark theme. break; case ASCScreenBrightnessStyleLight: // Do something else, e.g set a light theme. break; }
To continuously detect screen brightness changes implement ASCScreenBrightnessDetector as an instance variable, set the delegate and use the following delegate methods:
为了实时监测屏幕亮度的变化,你需要初始化ASCScreenBrightnessDetector这个实例变量,然后设置一个代理:
- (void)screenBrightnessDidChange:(CGFloat)brightness { NSLog(@"The new brightness is: %f", brightness); } - (void)screenBrightnessStyleDidChange:(ASCScreenBrightnessStyle)style { NSLog(@"The new style is: %u", style); }
The object that acts as the delegate.
这是一个代理对象。
id<ASCScreenBrightnessDetectorDelegate> delegate;
The brightness level of the screen between 0.0 and 1.0, inclusive. (read-only)
亮度的值是只读的,介于0.0与1.0之间(包括0.0与1.0)
CGFloat screenBrightness;
The style indicates if the screen brightness is dark or light and depends on the defined threshold. (read-only)
亮度指示器用以表示屏幕是亮的还是暗的(只读)
ASCScreenBrightnessStyle screenBrightnessStyle;
The threshold determines whether the brightness style is light or dark. It must have a value between 0.0 and 1.0, inclusive. The default value is 0.5.
这个值标示着明与暗之间的分割线,其值介于0.0到1.0之间,默认值是0.5。
CGFloat threshold;
Tells the delegate when the screens brightness changed and returns a float
value between 0.0 and 1.0, inclusive.
你可以从这个代理方法中获取到亮度的变化,介于0.0与1.0之间(包含0.0与1.0)
- (void)screenBrightnessDidChange:(CGFloat)brightness;
Tells the delegate when the screens brightness style changed and returns anASCScreenBrightnessStyle
enumeration.
你可以从这个代理方法中获取亮度风格的变化
- (void)screenBrightnessStyleDidChange:(ASCScreenBrightnessStyle)style;
ASCScreenBrightnessDetector is available through CocoaPods, to install it simply add the following line to your Podfile:
ASCScreenBrightnessDetector支持CocoaPods,你可以通过以下一句话来安装到Podfile当中:
pod "ASCScreenBrightnessDetector"
Drag the ASCScreenBrightnessDetector.h
and ASCScreenBrightnessDetector.m
source files to your project and you are done.
将ASCScreenBrightnessDetector.h与ASCScreenBrightnessDetector.m拖到你的项目当中。
André Schneider, @aschndr
ASCScreenBrightnessDetector is available under the MIT license. See the LICENSE file for more info.