我们有时后会遇到这样一个场景,当进入某一个界面的时候需要这个界面高亮显示,而其他界面则是正常的亮度。比如说,在使用支付宝收付款时,进入二维码展示界面时,页面会变高亮。
现在来看看具体的解决方案。
一. 首先 UIScreen 有一个属性 brightness 可以设置屏幕的亮度。值为 0 ~ 1之间,为1时亮度最高,0 时亮度最低。
二.我们需要定义几个宏,在pch文件或者其他文件中。
#define SystemBrightness @"systemBrightness" //用于记录系统亮度
#define Notification_SetBrightnessSystem @"Notification_SetBrightnessSystem" //设置屏幕为系统亮度的通知
#define Notification_SetBrightnessHighLight @"Notification_SetBrightnessHighLight" //设置屏幕高亮的通知
三. 我们需要高亮显示的那个页面 .m
#import "QRCodeController.h"
@interface QRCodeController ()
@end
@implementation QRCodeController
- (void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
//首先记录下当前系统的屏幕亮度,保存到偏好设置
[[NSUserDefaults standardUserDefaults] setFloat:[UIScreen mainScreen].brightness forKey:SystemBrightness];
[[NSUserDefaults standardUserDefaults] synchronize];
//添加设置屏幕亮度的两个通知,主要是为了解决应用间跳转,应用从此界面进入后台,或从后台直接进入此界面的一些问题
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setBrightnessSystem) name:Notification_SetBrightnessSystem object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setBrightnessHighLight) name:Notification_SetBrightnessHighLight object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//在这个控制器出现的时候,将屏幕设置为高亮
[self setBrightnessHighLight];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//在这个控制器将要消失的时候,将屏幕设置为原系统亮度
[self setBrightnessSystem];
}
//将屏幕设置为原系统亮度
- (void)setBrightnessSystem
{
[UIScreen mainScreen].brightness = [[NSUserDefaults standardUserDefaults] floatForKey:SystemBrightness];
}
//将屏幕设置为高亮
- (void)setBrightnessHighLight
{
//具体亮度可以根据自己的需求,自定义设置亮度值
[UIScreen mainScreen].brightness = 1;
}
四. 在AppDelegate监听APP的状态,适时发出通知,更改屏幕亮度
//APP将要进入后台,跳转到其他应用,双击home键,下拉通知栏等情况下会调用
- (void)applicationWillResignActive:(UIApplication *)application
{
//APP进入非活跃状态时设置发送设置屏幕为系统亮度通知
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_SetBrightnessSystem object:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//APP进入活跃状态时发送设置屏幕高亮通知
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_SetBrightnessHighLight object:nil];
}
按这几步走,就完美解决了设置某一界面高亮的问题了。
如有问题,欢迎评论交流!
版权声明:本文为zxw_xzr原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。