{"id":5793,"date":"2022-05-11T10:30:39","date_gmt":"2022-05-11T02:30:39","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5793"},"modified":"2022-05-11T10:30:39","modified_gmt":"2022-05-11T02:30:39","slug":"swift-%e8%ae%a1%e6%ad%a5%e5%99%a8cmpedometer%e7%9a%84%e4%bd%bf%e7%94%a8%ef%bc%88%e8%8e%b7%e5%8f%96%e7%94%a8%e6%88%b7%e6%ad%a5%e6%95%b0%e3%80%81%e8%b7%9d%e7%a6%bb%e3%80%81%e9%80%9f%e5%ba%a6%e7%ad%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5793","title":{"rendered":"Swift &#8211; \u8ba1\u6b65\u5668CMPedometer\u7684\u4f7f\u7528\uff08\u83b7\u53d6\u7528\u6237\u6b65\u6570\u3001\u8ddd\u79bb\u3001\u901f\u5ea6\u7b49\uff09"},"content":{"rendered":"\n<p><strong>1 CMPedometer\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<ul><li><strong>CMPedometer<\/strong>\uff08\u8ba1\u6b65\u5668\uff09\u662f <strong>CoreMotion <\/strong>\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u529f\u80fd\u7c7b\uff0c\u4e3b\u8981\u662f\u4fdd\u5b58\u6709\u5173\u6b65\u884c\uff08\u6216\u8dd1\u6b65\u3001\u722c\u697c\uff09\u7684\u4e00\u4e9b\u4fe1\u606f\u3002<\/li><li><strong>CMPedometer <\/strong>\u540c\u65f6\u80fd\u4fdd\u5b58\u8fd1<strong>7<\/strong>\u5929\u7684\u6b65\u6570\u8bb0\u5f55\uff0c\u5e76\u63d0\u4f9b\u4e86\u67e5\u8be2\u8fd1<strong>7<\/strong>\u5929\u5185\u4efb\u610f\u65f6\u95f4\u6bb5\u7684\u6b65\u6570\u4fe1\u606f\u3002<\/li><li>\u60f3\u8981\u4f7f\u7528 <strong>CMPedometer<\/strong>\uff0c\u7cfb\u7edf\u6700\u4f4e\u7248\u672c\u9700\u8981\u662f <strong>iOS8<\/strong>\uff08\u90e8\u5206\u529f\u80fd\u9700\u8981 <strong>iOS9<\/strong>\uff09<\/li><\/ul>\n\n\n\n<p><strong>2 \u4f7f\u7528\u6837\u4f8b<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u7a0b\u5e8f\u542f\u52a8\u540e\u5148\u5224\u65ad\u8bbe\u5907\u662f\u5426\u652f\u6301\u8ba1\u6b65\u5668\u3002 <\/p>\n\n\n\n<p>\uff082\uff09\u5982\u679c\u652f\u6301\u7684\u8bdd\u5219\u5b9e\u65f6\u83b7\u53d6\u5f53\u5929\u7684\u7528\u6237\u8fd0\u52a8\u6570\u636e\uff0c\u6bd4\u5982\uff1a\u8dd1\u6b65+\u8d70\u8def\u7684\u6b65\u6570\u3001\u8ddd\u79bb\u3001\u722c\u7684\u697c\u5c42\u6570\u3001\u4e0b\u7684\u697c\u5c42\u6570\u3001\u5f53\u524d\u79fb\u52a8\u901f\u5ea6\u3002\u5e76\u5c06\u8fd9\u4e9b\u6570\u636e\u4e0d\u65ad\u66f4\u65b0\u5230\u754c\u9762\u4e0a\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-1024x656.png\" alt=\"\" class=\"wp-image-5794\" width=\"470\" height=\"301\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-1024x656.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-300x192.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-768x492.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-830x532.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-230x147.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-350x224.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38-480x307.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-38.png 1246w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3 \u6837\u4f8b\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u7531\u4e8e\u5b89\u5168\u9650\u5236\uff0c\u9996\u5148\u6211\u4eec\u8981\u5728 <strong>info.plist<\/strong> \u6587\u4ef6\u4e2d\u52a0\u5165\u8bbf\u95ee\u7528\u6237\u5065\u5eb7\u548c\u8fd0\u52a8\u4fe1\u606f\u7684\u76f8\u5173\u63cf\u8ff0\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39-1024x652.png\" alt=\"\" class=\"wp-image-5795\" width=\"566\" height=\"359\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39-1024x652.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39-300x191.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39-768x489.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39-830x528.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39-230x146.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39-350x223.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-39.png 1062w\" sizes=\"(max-width: 566px) 100vw, 566px\" \/><\/figure><\/div>\n\n\n\n<p>\uff082\uff09\u6837\u4f8b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport CoreMotion\n \nclass ViewController: UIViewController {\n    \/\/\u7528\u4e8e\u663e\u793a\u5b9e\u65f6\u4fe1\u606f\n    @IBOutlet weak var textView: UITextView!\n     \n    \/\/\u8ba1\u6b65\u5668\u5bf9\u8c61\n    let pedometer = CMPedometer()\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n \n        \/\/\u5f00\u59cb\u8ba1\u6b65\u5668\u66f4\u65b0\n        startPedometerUpdates()\n    }\n     \n    \/\/ \u5f00\u59cb\u83b7\u53d6\u6b65\u6570\u8ba1\u6570\u636e\n    func startPedometerUpdates() {\n        \/\/\u5224\u65ad\u8bbe\u5907\u652f\u6301\u60c5\u51b5\n        guard CMPedometer.isStepCountingAvailable() else {\n            self.textView.text = \"\\n\u5f53\u524d\u8bbe\u5907\u4e0d\u652f\u6301\u83b7\u53d6\u6b65\u6570\\n\"\n            return\n        }\n         \n        \/\/\u83b7\u53d6\u4eca\u5929\u51cc\u6668\u65f6\u95f4\n        let cal = Calendar.current\n        var comps = cal.dateComponents([.year, .month, .day], from: Date())\n        comps.hour = 0\n        comps.minute = 0\n        comps.second = 0\n        let midnightOfToday = cal.date(from: comps)!\n         \n        \/\/\u521d\u59cb\u5316\u5e76\u5f00\u59cb\u5b9e\u65f6\u83b7\u53d6\u6570\u636e\n        self.pedometer.startUpdates (from: midnightOfToday, withHandler: { pedometerData, error in\n            \/\/\u9519\u8bef\u5904\u7406\n            guard error == nil else {\n                print(error!)\n                return\n            }\n             \n            \/\/\u83b7\u53d6\u5404\u4e2a\u6570\u636e\n            var text = \"---\u4eca\u65e5\u8fd0\u52a8\u6570\u636e---\\n\"\n            if let numberOfSteps = pedometerData?.numberOfSteps {\n                text += \"\u6b65\u6570: \\(numberOfSteps)\\n\"\n            }\n            if let distance = pedometerData?.distance {\n                text += \"\u8ddd\u79bb: \\(distance)\\n\"\n            }\n            if let floorsAscended = pedometerData?.floorsAscended {\n                text += \"\u4e0a\u697c: \\(floorsAscended)\\n\"\n            }\n            if let floorsDescended = pedometerData?.floorsDescended {\n                text += \"\u4e0b\u697c: \\(floorsDescended)\\n\"\n            }\n            if let currentPace = pedometerData?.currentPace {\n                text += \"\u901f\u5ea6: \\(currentPace)m\/s\\n\"\n            }\n            if let currentCadence = pedometerData?.currentCadence {\n                text += \"\u901f\u5ea6: \\(currentCadence)\u6b65\/\u79d2\\n\"\n            }\n             \n            \/\/\u5728\u7ebf\u7a0b\u4e2d\u66f4\u65b0\u6587\u672c\u6846\u6570\u636e\n            DispatchQueue.main.async{\n                self.textView.text = text\n            }\n        })\n    }\n     \n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1 CMPedometer\u4ecb\u7ecd CMPedometer\uff08\u8ba1\u6b65\u5668\uff09\u662f CoreMotion \u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u529f\u80fd\u7c7b\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5793"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5793"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5793\/revisions"}],"predecessor-version":[{"id":5796,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5793\/revisions\/5796"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}