iOS 计步器的实现
2016-01-11 16:25
441 查看
在iOS 使用CMStepCounter实现计步器功能。需要注意的是:需要iPhone5S及以上型号的手机和iOS7.0及以上的操作系统
首先了解一下API:
步数计数可用性
+
isStepCountingAvailable
开始和停止更新步数计数
–
startStepCountingUpdatesToQueue:updateOn:withHandler:
–
stopStepCountingUpdates
–
queryStepCountStartingFrom:to:toQueue:withHandler:
下边介绍具体的使用方法:
+ (void)getStepCounter:(void (^)(NSString * totalStep))step
{
__block
NSInteger stepNumber = 0;
if (![CMStepCounter
isStepCountingAvailable]) {
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"警告"
message:@"只支持iPhone5S及以上型号的手机"delegate:nil
cancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}
NSDate* nowDate = [NSDate
date];
NSDateFormatter *dateFormatter = [[NSDateFormatter
alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSArray *stringArray = [[dateFormatter
stringFromDate:nowDate] componentsSeparatedByString:@":"];
NSTimeInterval oneDay = [[stringArray
objectAtIndex:0]
integerValue] *3600 + [[stringArray
objectAtIndex:1]
integerValue] *60 + [[stringArray
objectAtIndex:2]
integerValue] ;
//获取指定时间段之前
NSDate* theDate = [nowDate
initWithTimeIntervalSinceNow: -oneDay];
NSOperationQueue * operationQueue = [[NSOperationQueue
alloc] init];
CMStepCounter * stepCounter = [[CMStepCounter
alloc] init];
//获取今天的步数
[stepCounter queryStepCountStartingFrom:theDate
to:nowDate toQueue:operationQueue
withHandler:^(NSInteger numberOfSteps,
NSError * _Nullable error) {
if (error) {
//暂时没有处理
}
else {
stepNumber = numberOfSteps;
}
}];
//开始计步
[stepCounter startStepCountingUpdatesToQueue:operationQueue
updateOn:1
withHandler: ^(NSInteger numberOfSteps,
NSDate *timestamp, NSError *error) {
if (error) {
//暂时没有处理
}
else {
step([NSString
stringWithFormat:@"%zd", stepNumber + numberOfSteps]);
}
}];
}
参考文献:http://www.tuicool.com/articles/MNfu63 http://blog.csdn.net/u011010305/article/details/48932235
首先了解一下API:
步数计数可用性
+
isStepCountingAvailable
开始和停止更新步数计数
–
startStepCountingUpdatesToQueue:updateOn:withHandler:
–
stopStepCountingUpdates
获取历史步数计数数据
–queryStepCountStartingFrom:to:toQueue:withHandler:
下边介绍具体的使用方法:
+ (void)getStepCounter:(void (^)(NSString * totalStep))step
{
__block
NSInteger stepNumber = 0;
if (![CMStepCounter
isStepCountingAvailable]) {
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"警告"
message:@"只支持iPhone5S及以上型号的手机"delegate:nil
cancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}
NSDate* nowDate = [NSDate
date];
NSDateFormatter *dateFormatter = [[NSDateFormatter
alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSArray *stringArray = [[dateFormatter
stringFromDate:nowDate] componentsSeparatedByString:@":"];
NSTimeInterval oneDay = [[stringArray
objectAtIndex:0]
integerValue] *3600 + [[stringArray
objectAtIndex:1]
integerValue] *60 + [[stringArray
objectAtIndex:2]
integerValue] ;
//获取指定时间段之前
NSDate* theDate = [nowDate
initWithTimeIntervalSinceNow: -oneDay];
NSOperationQueue * operationQueue = [[NSOperationQueue
alloc] init];
CMStepCounter * stepCounter = [[CMStepCounter
alloc] init];
//获取今天的步数
[stepCounter queryStepCountStartingFrom:theDate
to:nowDate toQueue:operationQueue
withHandler:^(NSInteger numberOfSteps,
NSError * _Nullable error) {
if (error) {
//暂时没有处理
}
else {
stepNumber = numberOfSteps;
}
}];
//开始计步
[stepCounter startStepCountingUpdatesToQueue:operationQueue
updateOn:1
withHandler: ^(NSInteger numberOfSteps,
NSDate *timestamp, NSError *error) {
if (error) {
//暂时没有处理
}
else {
step([NSString
stringWithFormat:@"%zd", stepNumber + numberOfSteps]);
}
}];
}
参考文献:http://www.tuicool.com/articles/MNfu63 http://blog.csdn.net/u011010305/article/details/48932235
相关文章推荐
- iOS 学习资料整理
- 【用于ios6】【ios7以后用新的方法】实现电子书的分页显示功能
- IOS动画相关
- IOS-TextField知多少
- IOS使用第三方工具(RegexKitLite)实现正则表达式
- 分享!分享!分享!网易新闻iOS版使用的18个开源组件
- iOS开发之Masonry使用详解
- iOS 笔记
- IOS 百度地图API
- iOS8新添加的左滑出现按钮组的方法
- IOS 设备信息读取
- 网上下载特效素材的使用
- 如何用NSURLSession替代NSURLConnection
- 常用图标及启动页尺寸大小
- iOS动态获取键盘高度方法
- 正则表达式在iOS中的运用
- iOS性能优化:Instruments使用实战
- IOS-多线程
- 基于IOS上MDM技术相关资料整理及汇总
- IOS——使用ScrollView和PageController实现引导页分页效果