一、简单介绍
亮度是UIScreen的一个浮点型属性,而UIScreen是一个单例,所以这个亮度是全局的,任何一个地方改动,整个手机的亮度都会改变。这个亮度在iOS5.0后被苹果开放,开发者可以很方便的使用它。在此之前,开发者只能通过自己设置假亮度来达到效果,其实就是覆盖蒙层,修改透明度(自己可以去试试)。
二、亮度属性
@property(nonatomic) CGFloat brightness NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // 0 .. 1.0, where 1.0 is maximum brightness. Only supported by main screen.
//获取亮度 CGFloat brightess = [[UIScreen mainScreen] brightness]; //设置亮度 [[UIScreen mainScreen] setBrightness:0.5];
三、使用场景
电子书中有一个非常重要的功能,就是用户可以随意的改变亮度。有一点需要注意的是,电子书的亮度应该和系统的亮度区别开,电子书模块的亮度不应该影响手机亮度,viewWillAppear和viewWillDisappear在进入前后台时是不会触发的,那么如何处理呢?
首先:通过一个归档实例属性存储电子书的亮度,这个需要KVO监听,监听用户通过滑动条实时改变电子书亮度并归档。
其次:在电子式将要显示时通过一个临时的变量记住系统的亮度,同时从解归档取出电子式亮度并设置。
然后:在电子式将要消失时取出这个临时的系统亮度,并复原手机亮度即可。
接着:监听是否触发home键挂起程序:applicationWillResignActive,取出这个临时的系统亮度,并复原手机亮度即可。
最后:监听是否重新进入程序程序:applicationDidBecomeActive,接着采用这个临时的变量记住系统的亮度,同时从解归档取出电子式亮度并设置。
四、使用方法
#import <Foundation/Foundation.h> //此类是一个单例,专门用来存储电子书屏幕亮度 @interface LSYReadConfig : NSObject<NSCoding> +(instancetype)shareInstance; @property (nonatomic,assign) CGFloat brightness; @end #import "LSYReadConfig.h" @implementation LSYReadConfig +(instancetype)shareInstance { static LSYReadConfig *readConfig = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ readConfig = [[self alloc] init]; }); return readConfig; } - (instancetype)init { self = [super init]; if (self) { NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"ReadConfig"]; if (data) { NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data]; LSYReadConfig *config = [unarchive decodeObjectForKey:@"ReadConfig"]; [config addObserver:config forKeyPath:@"brightness" options:NSKeyValueObservingOptionNew context:NULL]; return config; } _brightness = 0.65f; options:NSKeyValueObservingOptionNew context:NULL]; [self addObserver:self forKeyPath:@"brightness" options:NSKeyValueObservingOptionNew context:NULL]; [LSYReadConfig updateLocalConfig:self]; } return self; } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context { [LSYReadConfig updateLocalConfig:self]; } +(void)updateLocalConfig:(LSYReadConfig *)config { NSMutableData *data=[[NSMutableData alloc]init]; NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data]; [archiver encodeObject:config forKey:@"ReadConfig"]; [archiver finishEncoding]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"ReadConfig"]; } -(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeDouble:self.brightness forKey:@"brightness"]; } -(id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { self.brightness = [aDecoder decodeDoubleForKey:@"brightness"]; } return self; } @end
//用户拖动滑动条改变电子书亮度
-(void)changeScreenHighLight:(UISlider *)sender{
CGFloat currentLight = (CGFloat)sender.value;
[LSYReadConfig shareInstance].brightness = currentLight;
[[UIScreen mainScreen] setBrightness: currentLight];
}
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];
//记录手机系统亮度并设置电子书亮度 self.systemLight = [[UIScreen mainScreen] brightness]; [[UIScreen mainScreen] setBrightness:[[LSYReadConfig shareInstance] brightness]]; }
-(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated];
//设置手机系统亮度 [[UIScreen mainScreen] setBrightness:self.systemLight]; }
//监听是否触发home键挂起程序. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil]; //监听是否重新进入程序程序. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
//从电子书进入后台,复原手机亮度 -(void)applicationWillResignActive:(UIApplication *)application { [[UIScreen mainScreen] setBrightness:self.systemLight]; } //进入电子书前台,设置电子书亮度 -(void)applicationDidBecomeActive:(UIApplication *)application { self.systemLight = [[UIScreen mainScreen] brightness]; [[UIScreen mainScreen] setBrightness:[[LSYReadConfig shareInstance] brightness]]; }