码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 通过接受距离传感器的消息改变屏幕的明暗度(仅限用于真实的手机)

时间:2015-07-06 23:14:08      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

 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

 

iOS 通过接受距离传感器的消息改变屏幕的明暗度(仅限用于真实的手机)

标签:

原文地址:http://www.cnblogs.com/lantu1989/p/4625565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!