標籤:
感應器
實現代碼:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 1.開啟距離感應器(注意: 預設情況距離感應器是關閉的) // 只要開啟之後, 就開始即時監聽 [UIDevice currentDevice].proximityMonitoringEnabled = YES; // 2.當監聽到有物體靠近裝置時系統會發出通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil]; }// 註冊了通知,在不使用的時候,一定要銷毀- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}// 當監聽到有物體靠近裝置時調用- (void)proximityStateDidChange:(NSNotification *)note{ if( [UIDevice currentDevice].proximityState) { NSLog(@"有物體靠近"); }else { NSLog(@"物體離開"); }}@end搖一搖
/** * 開始搖晃就會調用 */- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"%s", __func__);}/** * 搖晃結束就會調用 */- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"%s", __func__);}/** * 搖晃被打斷就會調用 */- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"%s", __func__);}
iOS UI進階-5.0 藍芽/加速計/感應器