swift計(jì)步器CMPedometer的使用方法
最近公司接了個(gè)項(xiàng)目,是一款運(yùn)動(dòng)類(lèi)型的APP,可以檢測(cè)運(yùn)動(dòng)量(例如:步數(shù),上下樓等)、睡眠信息、速度等信息,因?yàn)橐郧按致缘牧私膺^(guò)傳感器方面的相關(guān)信息,知道主要是蘋(píng)果設(shè)備內(nèi)置的傳感器在起作用,傳感器的種類(lèi)也很多,有興趣的可以去查看蘋(píng)果官方文檔或者查閱大神們的博客都可以找到!但是一直也沒(méi)有自己寫(xiě)一下,做個(gè)測(cè)試:
話(huà)不多說(shuō),代碼如下:
1、準(zhǔn)備
// 計(jì)步器 var myTextView = UITextView() var pedometer = CMPedometer() var myBtn = UIButton()
2、在plist文件中添加相關(guān)權(quán)限描述:
Privacy - Motion Usage Description :String 計(jì)步器需要獲取您的運(yùn)動(dòng)信息
3、具體代碼如下:
myBtn.frame = CGRect(x:10,y:360,width:kScreenWidth-20,height:50)
myBtn.setTitle("按鈕", for: .normal)
myBtn.setTitle("倒計(jì)時(shí)中", for: .disabled)
myBtn.backgroundColor = UIColor.orange
myBtn.setTitleColor(UIColor.white, for: .normal)
myBtn.setTitleColor(UIColor.blue, for: .disabled)
myBtn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
self.view.addSubview(myBtn)
func btnClick(){
self.startPedometerUpdates()
}
// ---------------------------------計(jì)步器--------------------------------------------
func creatPedometer(){
myTextView.frame = CGRect(x:20,y:100,width:kScreenWidth-20,height:200)
self.view.addSubview(myTextView)
// 創(chuàng)建觸發(fā)按鈕
myBtn.frame = CGRect(x:10,y:360,width:kScreenWidth-20,height:50)
myBtn.setTitle("按鈕", for: .normal)
// myBtn.setTitle("倒計(jì)時(shí)中", for: .disabled)
myBtn.backgroundColor = UIColor.orange
// myBtn.setTitleColor(UIColor.white, for: .normal)
// myBtn.setTitleColor(UIColor.blue, for: .disabled)
myBtn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
self.view.addSubview(myBtn)
}
// 開(kāi)始獲取步數(shù)統(tǒng)計(jì)數(shù)據(jù)
func startPedometerUpdates(){
// 判斷設(shè)備支持情況
guard CMPedometer.isStepCountingAvailable() else {
self.myTextView.text = "\n當(dāng)前設(shè)備不支持獲取步數(shù)\n"
return
}
// 獲取今天凌晨時(shí)間
let cal = Calendar.current
var comps = cal.dateComponents([.year,.month,.day], from: Date())
comps.hour = 0
comps.minute = 0
comps.second = 0
let midnightOfToday = cal.date(from: comps)
//初始化并開(kāi)始實(shí)時(shí)獲取數(shù)據(jù)
self.pedometer.startUpdates (from: midnightOfToday!, withHandler: { pedometerData, error in
//錯(cuò)誤處理
guard error == nil else {
print(error!)
return
}
//獲取各個(gè)數(shù)據(jù)
var text = "---今日運(yùn)動(dòng)數(shù)據(jù)---\n"
if let numberOfSteps = pedometerData?.numberOfSteps {
text += "步數(shù): \(numberOfSteps)\n"
}
if let distance = pedometerData?.distance {
text += "距離: \(distance)\n"
}
if let floorsAscended = pedometerData?.floorsAscended {
text += "上樓: \(floorsAscended)\n"
}
if let floorsDescended = pedometerData?.floorsDescended {
text += "下樓: \(floorsDescended)\n"
}
if #available(iOS 9.0, *) {
if let currentPace = pedometerData?.currentPace {
text += "速度: \(currentPace)m/s\n"
}
} else {
// Fallback on earlier versions
}
if #available(iOS 9.0, *) {
if let currentCadence = pedometerData?.currentCadence {
text += "速度: \(currentCadence)步/秒\n"
}
} else {
// Fallback on earlier versions
}
// 在線程中更新文本框數(shù)據(jù)
DispatchQueue.main.async {
self.myTextView.text = text
}
})
然后,測(cè)試了下,親自去爬了3層樓梯,如下圖:

基本的實(shí)現(xiàn)就完成了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
舉例講解Swift編程中switch...case語(yǔ)句的用法
這篇文章主要介紹了Swift編程中switch...case語(yǔ)句的用法,其中fallthrough關(guān)鍵字在switch語(yǔ)句中的使用是重點(diǎn),需要的朋友可以參考下2016-04-04
蘋(píng)果公司編程語(yǔ)言Swift語(yǔ)言簡(jiǎn)介
這篇文章主要介紹了蘋(píng)果公司編程語(yǔ)言Swift語(yǔ)言簡(jiǎn)介,Swift 是一門(mén)新的編程語(yǔ)言,兼容Objective-C代碼,是用來(lái)代替Objective-C的蘋(píng)果主力開(kāi)發(fā)語(yǔ)言,需要的朋友可以參考下2014-07-07
swift版webview加載網(wǎng)頁(yè)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)webview加載網(wǎng)頁(yè)進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
解析Swift語(yǔ)言面相對(duì)象編程中的繼承特性
這篇文章主要介紹了解析Swift語(yǔ)言面相對(duì)象編程中的繼承特性,是Swift入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11
SwiftUI 中創(chuàng)建反彈動(dòng)畫(huà)的實(shí)現(xiàn)
這篇文章主要介紹了SwiftUI 中創(chuàng)建反彈動(dòng)畫(huà)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

