iOS 调节屏幕明暗度

1 #import "AppDelegate.h"
2
3 @interface AppDelegate ()
4
5 @end
6
7 @implementation AppDelegate
8
9
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12 // Override point for customization after application launch.
13 self.window.backgroundColor = [UIColor whiteColor];
14
15 UIDevice *device = [UIDevice currentDevice];
16 device.proximityMonitoringEnabled = YES;
17 if (device.proximityMonitoringEnabled == YES) {
18 [[NSNotificationCenter defaultCenter] addObserver:self
19 selector:@selector(proximityChanged:)
20 name:@"UIDeviceProximityStateDidChangeNotification" object:device];
21 }
22
23 [self.window makeKeyAndVisible];
24 return YES;
25 }
26
27 - (void)tuneBrightness:(NSTimer *)timer
28 {
29 if ([UIScreen mainScreen].brightness < 1) {
30 [UIScreen mainScreen].brightness += 0.05;
31 }else{
32 [timer invalidate];
33 }
34 }
35
36 - (void)proximityChanged:(NSNotification *)notification
37 {
38 UIDevice *device = [notification object];
39 if (!device.proximityState) {
40 [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(tuneBrightness:) userInfo:nil repeats:YES];
41 }
42 }
43
44
45 - (void)applicationWillTerminate:(UIApplication *)application {
46 // 删除Observer
47 [[NSNotificationCenter defaultCenter] removeObserver:self];
48
49 }
50
51 @end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值