目錄
一签舞、距離傳感器
二、加速計(jì)
三柒瓣、磁力計(jì)儒搭、陀螺儀的使用和上述加速計(jì)的使用步驟類似
四、搖一搖
五芙贫、步數(shù)
一搂鲫、距離傳感器
監(jiān)聽(tīng)方式:添加
觀察者
,監(jiān)聽(tīng)通知
通知名稱:
UIDeviceProximityStateDidChangeNotification
監(jiān)聽(tīng)狀態(tài):觀察者的對(duì)應(yīng)回調(diào)方法中,判斷
[UIDevice currentDevice].proximityState
?? 返回NO
:有物品靠近了
?? 返回YES
:有物品遠(yuǎn)離了注意:使用前要打開(kāi)當(dāng)前設(shè)備距離傳感器的開(kāi)關(guān)(默認(rèn)為:NO):
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
- 示例程序:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
- (void)proximityStateDidChange
{
if ([UIDevice currentDevice].proximityState) {
NSLog(@"有物品靠近");
} else {
NSLog(@"有物品遠(yuǎn)離");
}
}
二磺平、加速計(jì)
- 概述:
檢測(cè)設(shè)備在X/Y/Z軸的受力情況
- 備注:
UIAcceleration
和UIAccelerometer
在iOS 5.0中寂靜被棄用魂仍。由Core Motion framework
取代
Core Moton 獲取數(shù)據(jù)的兩種方式:
- push:實(shí)時(shí)采集所有數(shù)據(jù),采集頻率高拣挪;
- pull:在有需要的時(shí)候才去采集數(shù)據(jù)擦酌;
1)push方式
實(shí)例程序:
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (nonatomic, strong) CMMotionManager *mgr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. 判斷加速計(jì)是否好用
if (!self.mgr.isAccelerometerAvailable) {
NSLog(@"加速計(jì)不好用");
return;
}
// 2. 設(shè)置采樣間隔
self.mgr.accelerometerUpdateInterval = 1.0 / 30.0; // 1秒鐘采樣30次
// 3. 開(kāi)始采樣
[self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
// 當(dāng)采樣到加速計(jì)信息時(shí)就會(huì)執(zhí)行
if (error) return;
// 4.獲取加速計(jì)信息
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}];
}
- (CMMotionManager *)mgr
{
if (!_mgr) {
_mgr = [[CMMotionManager alloc] init];
}
return _mgr;
}
@end
2)pull方式
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (nonatomic, strong) CMMotionManager *mgr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. 判斷加速計(jì)是否好用
if (!self.mgr.isAccelerometerAvailable) {
NSLog(@"加速計(jì)不好用");
return;
}
// 2. 開(kāi)始采樣
[self.mgr startAccelerometerUpdates];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;
NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}
- (CMMotionManager *)mgr
{
if (!_mgr) {
_mgr = [[CMMotionManager alloc] init];
}
return _mgr;
}
@end
三、磁力計(jì)菠劝、陀螺儀的使用和上述加速計(jì)的使用步驟類似
不同點(diǎn):
- 判斷傳感器是否可用:
加速計(jì)
@property(readonly, nonatomic, getter=isAccelerometerAvailable) BOOL accelerometerAvailable;
陀螺儀
@property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable;
磁力計(jì)
@property(readonly, nonatomic, getter=isMagnetometerAvailable) BOOL magnetometerAvailable
- 2.設(shè)置傳感器的采樣間隔:
加速計(jì)
@property(assign, nonatomic) NSTimeInterval accelerometerUpdateInterval;
陀螺儀
@property(assign, nonatomic) NSTimeInterval gyroUpdateInterval;
磁力計(jì)
@property(assign, nonatomic) NSTimeInterval magnetometerUpdateInterval
- 3.1 開(kāi)始采樣的方法
push
:
加速計(jì)
- (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler;
陀螺儀
- (void)startGyroUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMGyroHandler)handler;
磁力計(jì)
- (void)startMagnetometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMagnetometerHandler)handler;
- 3.2
pull
方式
加速計(jì)
- (void)startAccelerometerUpdates;
陀螺儀
- (void)startGyroUpdates;
磁力計(jì)
- (void)startMagnetometerUpdates;
- 4.1 獲取采樣的數(shù)據(jù) push
在對(duì)應(yīng)的傳感器的開(kāi)始采樣方法中的handler
中 - 4.2 pull方式
加速計(jì)
CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;
NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
陀螺儀
CMRotationRate rate = self.mgr.gyroData.rotationRate;
NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
磁力計(jì)
CMMagneticField magneticField = self.mgr.magnetometerData.magneticField;
NSLog(@"x:%f y:%f z:%f",magneticField.x, magneticField.y, magneticField.z);
還有個(gè)停止方法
// 加速計(jì)赊舶、陀螺儀、磁力計(jì)都類似
- (void)stopAccelerometerUpdates
四赶诊、搖一搖
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void) viewWillAppear:(BOOL)animated
{
[self resignFirstResponder];
[super viewWillAppear:animated];
}
/*
解釋一下
在自定義的UIView子類中笼平,需要實(shí)現(xiàn)canBecomeFirstResponder方法,并返回YES(默認(rèn)返回FALSE)舔痪,
才可使becomeFirstResponder可返回YES,才可使其成為第一響應(yīng)者出吹,即接受第一響應(yīng)者狀態(tài)。
一個(gè)響應(yīng)者只有當(dāng)當(dāng)前響應(yīng)者可以取消第一響應(yīng)者狀態(tài) (canResignFirstResponder) 并且新的響應(yīng)者可以成為第一響應(yīng)者時(shí)辙喂,才可以成為第一響應(yīng)者捶牢。
*/
-(BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"搖一搖");
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"搖一搖被取消");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"搖一搖停止");
}
模擬器中搖一搖動(dòng)作
五鸠珠、步數(shù)
if (![CMPedometer isStepCountingAvailable]) {
NSLog(@"計(jì)步器不可用");
return;
}
CMPedometer *stepCounter = [[CMPedometer alloc] init];
[stepCounter startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData *pedometerData, NSError *error) {
if (error) return;
// 4.獲取采樣數(shù)據(jù),實(shí)時(shí)走
NSLog(@"steps = %@", pedometerData.numberOfSteps);
}];
統(tǒng)計(jì)今天走了多少步秋麸,多遠(yuǎn)渐排,垂直,室內(nèi)導(dǎo)航等
http://www.reibang.com/p/e5f332f9b27c
還可通過(guò)HealthKit框架獲取今日步數(shù), 參考以下鏈接
http://www.reibang.com/p/913013a226aa
NSDate *now = [NSDate date];
NSCalendar *calender = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponent = [calender components:unitFlags fromDate:now];
int hour = (int)[dateComponent hour];
int minute = (int)[dateComponent minute];
int second = (int)[dateComponent second];
NSDate *nowDay = [NSDate dateWithTimeIntervalSinceNow: - (hour*3600 + minute * 60 + second) ];
NSDate *nextDay = [NSDate dateWithTimeIntervalSinceNow: - (hour*3600 + minute * 60 + second) + 86400];
[_pedometer queryPedometerDataFromDate:nowDay toDate:nextDay withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
if (error) {
NSLog(@"error====%@",error);
}else {
NSLog(@"步數(shù)====%@",pedometerData.numberOfSteps);
NSLog(@"距離====%@米",pedometerData.distance);
}
}];
六灸蟆、藍(lán)牙
以后更新
參考
http://blog.csdn.net/andy_jiangbin/article/details/9991335
http://www.reibang.com/p/233be81b8ead
https://segmentfault.com/a/1190000000476207