一、簡單介紹
亮度是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]]; }