ASCScreenBrightnessDetector
ASCScreenBrightnessDetector copied to clipboard
Detects screen brightness changes and provides some useful delegate methods.
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:
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.
Wherever you want to use ASCScreenBrightnessDetector, import the header file as follows:
#import "ASCScreenBrightnessDetector.h"
or when using 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:
- (void)screenBrightnessDidChange:(CGFloat)brightness
{
NSLog(@"The new brightness is: %f", brightness);
}
- (void)screenBrightnessStyleDidChange:(ASCScreenBrightnessStyle)style
{
NSLog(@"The new style is: %u", style);
}
Properties
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)
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.
CGFloat threshold;
Delegate Methods
Tells the delegate when the screens brightness changed and returns a float
value between 0.0 and 1.0, inclusive.
- (void)screenBrightnessDidChange:(CGFloat)brightness;
Tells the delegate when the screens brightness style changed and returns an ASCScreenBrightnessStyle
enumeration.
- (void)screenBrightnessStyleDidChange:(ASCScreenBrightnessStyle)style;
Installation
From CocoaPods
ASCScreenBrightnessDetector is available through CocoaPods, to install it simply add the following line to your Podfile:
pod "ASCScreenBrightnessDetector"
Manually
Drag the ASCScreenBrightnessDetector.h
and ASCScreenBrightnessDetector.m
source files to your project and you are done.
Author
André Schneider, @_schneiderandre
License
ASCScreenBrightnessDetector is available under the MIT license. See the LICENSE file for more info.