iOS CMPedometer 计步器

CMPedometer####

CMPedometeri:统计某段时间内用户步数,距离信息,甚至计算用户爬了多少级楼梯 在iOS8.0及以后系统可以使用
要使用CMPedometeri 需要我们在对应类中导入CoreMotion

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property(nonatomic,strong)CMPedometer *pedometer;
@end

Objective-C:ViewDidLoad方法里

//初始化
    self.pedometer = [[CMPedometer alloc]init];
    
    //判断记步功能
    if ([CMPedometer isStepCountingAvailable]) {
        [_pedometer queryPedometerDataFromDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*2] toDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*1] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
            if (error) {
                NSLog(@"error====%@",error);
            }else {
                NSLog(@"步数====%@",pedometerData.numberOfSteps);
                NSLog(@"距离====%@",pedometerData.distance);
            }
        }];
    }else{
        NSLog(@"记步功能不可用");
    }

Swift:

import CoreMotion 
 
let lengthFormatter = NSLengthFormatter() 
let pedometer = CMPedometer() 
pedometer.startPedometerUpdatesFromDate(NSDate(), withHandler: { data, error in 
    if !error { 
        println("Steps Taken: \(data.numberOfSteps)") 
 
        let distance = data.distance.doubleValue 
        println("Distance: \(lengthFormatter.stringFromMeters(distance))") 
 
        let time = data.endDate.timeIntervalSinceDate(data.startDate) 
        let speed = distance / time 
        println("Speed: \(lengthFormatter.stringFromMeters(speed)) / s") 
    } 
}) 

在支持的设备上,CMPedometer对floorsAscended/ floorsDescended的统计可使用CMAltimeter进行扩充,以获得更精细的垂直距离:

CMAltimeter###

Swift

import CoreMotion 
let altimeter = CMAltimeter() 
if CMAltimeter.isRelativeAltitudeAvailable() { 
    altimeter.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { data, error in 
        if !error { 
            println("Relative Altitude: \(data.relativeAltitude)") 
        } 
    }) 
} 

Objective-C

self.altimeter = [[CMAltimeter alloc]init];
    
    if ([CMAltimeter isRelativeAltitudeAvailable]) {
        [self.altimeter startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData * _Nullable altitudeData, NSError * _Nullable error) {
            if (error) {
                NSLog(@"error====%@",error);
            }else{
                NSLog(@"Relative Altimeter:%@",altitudeData.relativeAltitude);
            }
        }];
    }

CLFloor

CLFloor是iOS 8中的新API,CoreMotion中的新功能体现了苹果公司的雄心勃勃的室内导航计划。这些信息将会在本地化导航应用中扮演重要的角色。
Swift

import CoreLocation 
 
class LocationManagerDelegate: NSObject, CLLocationManagerDelegate { 
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) { 
        let location: CLLocation? = locations[0] as? CLLocation 
        if let floor: CLFloor? = location?.floor { 
            println("Current Floor: \(floor?.level)") 
        } 
    } 
} 
let manager = CLLocationManager() 
manager.delegate = LocationManagerDelegate() 
manager.startUpdatingLocation() 

Objective-C

#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager *manager;

 //定位管理类
    self.manager = [[CLLocationManager alloc]init];
    
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务当前可能尚未打开,请设置打开!");
        return;
    }else{
        CLLocation *location = [[CLLocation alloc]initWithLatitude:39.0f longitude:116.0f];
        CLFloor *floor = location.floor;
        NSLog(@"current Floor:%ld",(long)floor.level);
    }

 



作者:刘高见
链接:https://www.jianshu.com/p/e5f332f9b27c
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值