当前位置: 首页 > 工具软件 > Commotion > 使用案例 >

Commotion框架 —— CMPedometer 统计步数的使用

谭浩皛
2023-12-01

对于HealthKit这个框架,很多开发者都不陌生,通过读取HealthKit我们可以获取到运动的信息。追本溯源,这些信息是由CoreMotion提供的。

CoreMotion有陀螺仪和加速器的功能,里面的一些类和接口也是基于这两个功能,系统帮我们计算出来的,比如步数统计和运动状态的判断。这里说一下CoreMotion的一个类 CMPedometer。

CMPedometer 主要是保存有关步行(包括跑步)的一些信息,能保存近7天的步数记录。并提供了查询近七天内任意时间段的步数信息。

 + (BOOL)isStepCountingAvailable;//判断是否可以支持获取步数
 + (BOOL)isDistanceAvailable;//判断是否可以支持返回步行的距离
 + (BOOL)isFloorCountingAvailable;//判断是否可以支持爬楼梯的检测
/*
    这个方法用来查询近7天内的任意时间段的CMPedometerData信息。
    其中CMPedometerData  包含步数等信息。在下面使用的时候介绍。
*/
- (void)queryPedometerDataFromDate:(NSDate *)start
                            toDate:(NSDate *)end
                            withHandler:(CMPedometerHandler)handler;
/*
    当你的步数有更新的时候,会触发这个方法,返回从某一时刻开始到现在所有的信息统计CMPedometerData。
    值得一提的是这个方法不会和时时返回结果,每次刷新数据大概在一分钟左右。
*/
- (void)startPedometerUpdatesFromDate:(NSDate *)start
                          withHandler:(CMPedometerHandler)handler;

具体的使用:

1、记录步数

由于startPedometerUpdatesFromDate会持续返回数据,所以我们不需要反复调用。所以可以放在一个单例中。

+ (instancetype)sharedStepsManger {

    static PXStepsManger *stepsManger;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        stepsManger = [[PXStepsManger alloc] init];
    });
    return stepsManger;
}

- (instancetype)init{
    self = [super init];
    if(self){
        self.pedometer = [[CMPedometer alloc]init];
        [self.pedometer startPedometerUpdatesFromDate:[NSDate beginOfToday:[NSDate date]] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
            if (error) {
                NSLog(@"%@",error);
            }else{
               NSLog(@"开始时间:%@",pedometerData.startDate);
                NSLog(@"结束时间:%@",pedometerData.endDate);
                NSLog(@"步数:%@",pedometerData.numberOfSteps);
                NSLog(@"距离:%@",pedometerData.distance);
                NSLog(@"上楼层数:%@",pedometerData.floorsAscended);
                NSLog(@"下楼层数:%@",pedometerData.floorsDescended);
                NSLog(@"当前速度:%@",pedometerData.currentPace);//iOS9及其以上才可用
            }
        }];
    }
    return self;
}

2、获取指定时间段的步数【这里以获取近7天的数据为例】

这和上面的不同,是当你请求的时候才会触发

- (void)stepsRecordOfRecentlyWeek:(WeekStepsRecord)weekStepsRecord{
    //获取一周
    NSDate *todayBeginning = [NSDate beginOfToday:[NSDate date]];
    NSDate *todayending = [NSDate endOfDay:[NSDate date]];
    NSMutableArray *stepsArray = [NSMutableArray array];
    NSMutableArray *datesArray = [NSMutableArray array];
    for (int i = 6 ; i >= 0; i--) {
    //获取步数 传值为每一天的开始时间和结束时间。
        [self.pedometer queryPedometerDataFromDate:[todayBeginning dateByAddingDays:-i] toDate:[todayending dateByAddingDays:-i] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {

            [stepsArray addObject:pedometerData.numberOfSteps?pedometerData.numberOfSteps:@0];
            (stepsArray.count == 7)?            weekStepsRecord(stepsArray):nil;
        }];
    }
}

以上就是CMPedometer的使用方法,有关Commotion框架还有很多知识,欢迎大家和我一起讨论,相互学习,共同进步。

 类似资料: