IOS 計步器,ios計步器

來源:互聯網
上載者:User

IOS 計步器,ios計步器

這篇部落格介紹的是當前比較流行的“計步器”-只是簡單的知識點

計步器的實現在IOS8開始進行了改變。

但是我會對之前之後的都進行簡單介紹。

IOS 8 -

////  ViewController.m//  CX 計步器////  Created by ma c on 16/4/12.//  Copyright © 2016年 bjsxt. All rights reserved.//#import "ViewController.h"#import <CoreMotion/CoreMotion.h>@interface ViewController ()@property (nonatomic, strong) CMStepCounter * counter;@end@implementation ViewController#pragma mark - <懶載入>- (CMStepCounter *)counter{    if (!_counter) {        _counter = [[CMStepCounter alloc]init];    }    return _counter;}- (void)viewDidLoad {    [super viewDidLoad];        //判斷計步器是否可用    if (![CMStepCounter isStepCountingAvailable]) {        NSLog(@"計步器不可用");        return;    }    //開始計步    //startStepCountingUpdatesToQueue 選擇在哪個線程裡進行    //updateOn 對應的是每隔幾秒後回調下面的block    [self.counter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:2 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {                if (error) {            return ;        }                NSLog(@"可以在這裡進行一些操作");            }];}@end

IOS 8 +

////  ViewController.m//  CX 計步器////  Created by ma c on 16/4/12.//  Copyright © 2016年 bjsxt. All rights reserved.//#import "ViewController.h"#import <CoreMotion/CoreMotion.h>@interface ViewController ()@property (nonatomic, strong) CMPedometer * meter;@end@implementation ViewController#pragma mark - <懶載入>- (CMPedometer *)meter{    if (!_meter) {        _meter = [[CMPedometer alloc]init];    }    return _meter;}- (void)viewDidLoad {    [super viewDidLoad];        //判斷計步器是否可用    if (![CMPedometer isStepCountingAvailable]) {        NSLog(@"計步器不可用");        return;    }    //開始計步    //在一段時間內的部步數與距離    [self.meter queryPedometerDataFromDate:[NSDate date] toDate:[NSDate dateWithTimeIntervalSinceNow:60*60] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {        if (error)        {            NSLog(@"error===%@",error);        }        else {            NSLog(@"步數===%@",pedometerData.numberOfSteps);            NSLog(@"距離===%@",pedometerData.distance);        }    }];}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.