{"id":5797,"date":"2022-05-11T10:33:47","date_gmt":"2022-05-11T02:33:47","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5797"},"modified":"2022-05-11T10:33:47","modified_gmt":"2022-05-11T02:33:47","slug":"swift-%e6%b4%bb%e5%8a%a8%e5%99%a8cmmotionactivitymanager%e7%9a%84%e4%bd%bf%e7%94%a8%ef%bc%88%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e7%9a%84%e8%bf%90%e5%8a%a8%e7%8a%b6%e6%80%81%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5797","title":{"rendered":"Swift &#8211; \u6d3b\u52a8\u5668CMMotionActivityManager\u7684\u4f7f\u7528\uff08\u83b7\u53d6\u5f53\u524d\u7684\u8fd0\u52a8\u72b6\u6001\uff09"},"content":{"rendered":"\n<p><strong>1 CMMotionActivityManager\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<p>\u6d3b\u52a8\u5668 <strong>CMMotionActivityManager<\/strong> \u662f <strong>CoreMotion<\/strong> \u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u529f\u80fd\u7c7b\uff0c\u4e3b\u8981\u7528\u4e8e\u68c0\u67e5\u7528\u6237\u5f53\u524d\u7684\u6d3b\u52a8\u72b6\u6001\u3002<\/p>\n\n\n\n<p> \u603b\u5171\u53ef\u4ee5\u68c0\u6d4b\u5230 <strong>5<\/strong> \u79cd\u72b6\u6001\uff1a<strong>\u9759\u6b62<\/strong>\u3001<strong>\u6b65\u884c<\/strong>\u3001<strong>\u8dd1\u6b65<\/strong>\u3001<strong>\u81ea\u884c\u8f66<\/strong>\u3001<strong>\u9a7e\u8f66<\/strong>\u3002<\/p>\n\n\n\n<p> \u540c\u65f6\u6bcf\u6b21\u8fd4\u56de\u7ed3\u679c\u65f6\uff0c\u90fd\u4f1a\u9644\u5e26\u672c\u6b21\u7ed3\u679c\u51c6\u786e\u7a0b\u5ea6\u7684\u63cf\u8ff0\uff0c\u5171\u6709\uff1a<strong>\u4f4e<\/strong> \u3001<strong>\u4e2d<\/strong> \u3001<strong>\u9ad8<\/strong>\u4e09\u4e2a\u7b49\u7ea7\u3002<\/p>\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\u6d3b\u52a8\u5668\uff08\u9700\u89815S\u6216\u4ee5\u540e\u7684\u8bbe\u5907\uff09\u3002<\/p>\n\n\n\n<p> \uff082\uff09\u5982\u679c\u652f\u6301\u7684\u8bdd\u5219\u5b9e\u65f6\u83b7\u53d6\u5f53\u524d\u7684\u8fd0\u52a8\u72b6\u6001\u4ee5\u53ca\u7cbe\u786e\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-40-1024x464.png\" alt=\"\" class=\"wp-image-5798\" width=\"548\" height=\"248\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40-1024x464.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40-300x136.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40-768x348.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40-830x376.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40-230x104.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40-350x159.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40-480x217.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-40.png 1258w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/figure><\/div>\n\n\n\n<p><br><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-41-1024x657.png\" alt=\"\" class=\"wp-image-5799\" width=\"530\" height=\"339\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41-1024x657.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41-300x192.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41-768x492.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41-830x532.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41-230x147.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41-350x224.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41-480x308.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-41.png 1070w\" sizes=\"(max-width: 530px) 100vw, 530px\" \/><\/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    \/\/\u6d3b\u52a8\u5668\u5bf9\u8c61\n    let motionActivityManager = CMMotionActivityManager()\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u5f00\u59cb\u83b7\u53d6\u6d3b\u52a8\u72b6\u6001\n        startActivityUpdates()\n    }\n     \n    \/\/ \u5f00\u59cb\u83b7\u53d6\u6d3b\u52a8\u5668\u6570\u636e\n    func startActivityUpdates() {\n        \/\/\u5224\u65ad\u8bbe\u5907\u652f\u6301\u60c5\u51b5\n        guard CMMotionActivityManager.isActivityAvailable() else {\n            self.textView.text = \"\\n\u5f53\u524d\u8bbe\u5907\u4e0d\u652f\u6301\u83b7\u53d6\u5f53\u524d\u8fd0\u52a8\u72b6\u6001\\n\"\n            return\n        }\n         \n        \/\/\u521d\u59cb\u5316\u5e76\u5f00\u59cb\u5b9e\u65f6\u83b7\u53d6\u6570\u636e\n        let queue = OperationQueue.current\n        self.motionActivityManager.startActivityUpdates(to: queue!, withHandler: {\n            activity in\n            \/\/\u83b7\u53d6\u5404\u4e2a\u6570\u636e\n            var text = \"---\u6d3b\u52a8\u5668\u6570\u636e---\\n\"\n            text += \"\u5f53\u524d\u72b6\u6001: \\(activity!.getDescription())\\n\"\n            if (activity!.confidence == .low) {\n                text += \"\u51c6\u786e\u5ea6: \u4f4e\\n\"\n            } else if (activity!.confidence == .medium) {\n                text += \"\u51c6\u786e\u5ea6: \u4f4e\\n\"\n            } else if (activity!.confidence == .high) {\n                text += \"\u51c6\u786e\u5ea6: \u9ad8\\n\"\n            }\n            self.textView.text = text\n        })\n    }\n     \n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}\n \nextension CMMotionActivity {\n    \/\/\/ \u83b7\u53d6\u7528\u6237\u8bbe\u5907\u5f53\u524d\u6240\u5904\u73af\u5883\u7684\u63cf\u8ff0\n    func getDescription() -> String {\n        if self.stationary {\n            return \"\u9759\u6b62\"\n        } else if self.walking {\n            return \"\u6b65\u884c\"\n        } else if self.running {\n            return \"\u8dd1\u6b65\"\n        } else if self.automotive {\n            return \"\u9a7e\u8f66\"\n        }else if self.cycling {\n            return \"\u81ea\u884c\u8f66\"\n        }\n        return \"\u672a\u77e5\"\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1 CMMotionActivityManager\u4ecb\u7ecd \u6d3b\u52a8\u5668 CMMotionActivityManager [&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\/5797"}],"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=5797"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5797\/revisions"}],"predecessor-version":[{"id":5800,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5797\/revisions\/5800"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}