1.首先,在需要实现的界面viewDidLoad中先保存亮度没有变化之前的亮度,因为这个界面消失之后还要变回原来的亮度,变化亮度也相当于用户的一种设置,所以也要保存一下这个状态,因为之后当APP在active和非active切换时候需要知道是否需要为用户做这个功能
1
2
3
4
|
// 保存原来的亮度 [[NSUserDefaults standardUserDefaults] setObject:@([UIScreen mainScreen].brightness) forKey:@ "brightness" ]; // 保存状态,需要为用户变化屏幕亮度功能 [[NSUserDefaults standardUserDefaults] setObject: @YES forKey:@ "ifNeedChangeLight" ]; |
2.在viewDidAppear中设置需要的亮度
1
2
3
4
5
6
7
|
- ( void )viewDidAppear:(BOOL)animated { [ super viewDidAppear:animated]; if ([UIScreen mainScreen].brightness < 0.8 ) { [UIScreen mainScreen].brightness = 0.8 ; } } |
3.同理viewDidDisappear
1
2
3
4
5
6
7
|
- ( void ):(BOOL)animated { [ super viewDidDisappear:animated]; // 还原 [UIScreen mainScreen].brightness = [[[NSUserDefaults standardUserDefaults] objectForKey:@ "brightness" ] floatValue]; [[NSUserDefaults standardUserDefaults] setObject: @NO forKey:@ "ifNeedChangeLight" ]; } |
4.当从底部滑起操作栏和退出app到主界面需要进行恢复和设置相应操作
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- ( void )applicationWillResignActive:(UIApplication *)application { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@ "ifNeedChangeLight" ] boolValue]) { [UIScreen mainScreen].brightness = [[[NSUserDefaults standardUserDefaults] objectForKey:@ "brightness" ] floatValue]; } } - ( void )applicationDidBecomeActive:(UIApplication *)application { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@ "ifNeedChangeLight" ] boolValue]) { if ([UIScreen mainScreen].brightness < 0.8 ) { // 颜色需要渐变的话需要另外再写 [UIScreen mainScreen].brightness = 0.8 ; } } } |
5.如果需要亮度渐变效果网上有实现的函数,设置亮度直接调函数就可以了
以上所述是小编给大家介绍的IOS 仿支付宝支付屏幕亮度变化机制,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/yuqingzhude/article/details/54135260