{"id":5805,"date":"2022-05-11T10:48:59","date_gmt":"2022-05-11T02:48:59","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5805"},"modified":"2022-05-11T10:48:59","modified_gmt":"2022-05-11T02:48:59","slug":"swift-%e7%a3%81%e5%8a%9b%e8%ae%a1%e6%95%b0%e6%8d%ae%e7%9a%84%e8%8e%b7%e5%8f%96%ef%bc%88%e9%99%84%e6%a0%b7%e4%be%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5805","title":{"rendered":"Swift &#8211; \u78c1\u529b\u8ba1\u6570\u636e\u7684\u83b7\u53d6\uff08\u9644\u6837\u4f8b\uff09"},"content":{"rendered":"\n<p>\u4f7f\u7528 <strong>CoreMotion <\/strong>\u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u83b7\u53d6\u8bbe\u5907\u5404\u79cd\u4f20\u611f\u5668\u7684\u6570\u636e\uff08\u6bd4\u5982\uff1a\u52a0\u901f\u5668\u3001\u9640\u87ba\u4eea\u3001\u78c1\u529b\u8ba1\uff09\u3002\u4e0a\u6587\u4e2d\u6211\u4ecb\u7ecd\u4e86\u9640\u87ba\u4eea\uff08<a rel=\"noreferrer noopener\" href=\"https:\/\/www.hangge.com\/blog\/cache\/detail_1548.html\" target=\"_blank\">\u70b9\u51fb\u67e5\u770b<\/a>\uff09\uff0c\u672c\u6587\u63a5\u7740\u8bb2\u89e3\u5982\u4f55\u83b7\u53d6\u78c1\u529b\u8ba1\u76f8\u5173\u6570\u636e\u3002<br><strong>\u4e00\u3001\u76f8\u5173\u8bf4\u660e<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1 \u78c1\u529b\u8ba1\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<p>\u78c1\u529b\u8ba1\uff08\u53c8\u53eb\u78c1\u529b\u4f20\u611f\u5668\u3001\u78c1\u529b\u4eea\u3001\u78c1\u573a\u8ba1\u3001\u78c1\u5f3a\u8ba1\uff09\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u611f\u5e94\u5468\u8fb9\u7684\u78c1\u573a\u60c5\u51b5\u3002 <\/p>\n\n\n\n<p>\u5176\u83b7\u53d6\u7684\u6570\u636e\u5305\u542b\u4e09\u8f74\u78c1\u529b\u503c\u3002 <\/p>\n\n\n\n<p>iPad\u7684\u76d2\u76d6\u9501\u5c4f\u529f\u80fd\u5c31\u662f\u901a\u8fc7\u78c1\u529b\u8ba1\u5b9e\u73b0\u3002\u540c\u65f6\u5b83\u53ef\u4ee5\u611f\u5e94\u5730\u7403\u78c1\u573a\uff0c \u83b7\u5f97\u65b9\u5411\u4fe1\u606f\uff0c \u4f7f\u4f4d\u7f6e\u670d\u52a1\u6570\u636e\u66f4\u7cbe\u51c6\uff0c\u5e38\u7528\u4e8e\u7535\u5b50\u7f57\u76d8\u548c\u5bfc\u822a\u5e94\u7528\u3002<\/p>\n\n\n\n<p><strong>2 CMMotionManager\u7c7b<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u901a\u8fc7 <strong>CMMotionManager <\/strong>\u7c7b\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u52a0\u901f\u5668\uff0c\u9640\u87ba\u4eea\uff0c\u78c1\u529b\u4eea\uff0c\u4f20\u611f\u5668\u8fd9<strong>4<\/strong>\u7c7b\u6570\u636e\uff0c\u540c\u65f6\u5b83\u4eec\u7684\u63a5\u53e3\u662f\u4e00\u81f4\u7684\u3002 <\/p>\n\n\n\n<p>\uff082\uff09<strong>CMMotionManager <\/strong>\u540c\u65f6\u63d0\u4f9b\u4e86&#8221;\u62c9&#8221;\u548c&#8221;\u63a8&#8221;\u4e24\u79cd\u65b9\u5f0f\u7684\u83b7\u53d6\u65b9\u5f0f\u3002\u4e8c\u8005\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>\u901a\u8fc7\u83b7\u53d6\u5f53\u524d\u4efb\u4f55\u4f20\u611f\u5668\u7684\u72b6\u6001\u6216\u662f <strong>CoreMotionManager <\/strong>\u7684\u53ea\u8bfb\u5c5e\u6027\u7ec4\u5408\u6570\u636e\u6765\u201c\u62c9\u201d\u52a8\u6001\u6570\u636e\u3002<\/li><li>\u901a\u8fc7 <strong>block <\/strong>\u95ed\u5305\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u6765\u83b7\u53d6\u4f60\u60f3\u8981\u7684\u66f4\u65b0\u6570\u636e\u96c6\u5408\u6765\u6355\u83b7\u201c\u63a8\u201d\u6570\u636e\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u4e8c\u3001\u4f7f\u7528\u6837\u4f8b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1 \u9700\u8981\u7684\u65f6\u5019\u624d\u53bb\u91c7\u96c6\u6570\u636e<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u6548\u679c\u56fe <\/p>\n\n\n\n<p>\u8fd9\u4e2a\u4fbf\u662f\u524d\u9762\u8bb2\u7684\u62c9\u6570\u636e\uff08<strong>pull<\/strong>\uff09\u3002\u6211\u4eec\u6bcf\u6b21\u70b9\u51fb\u201c<strong>\u83b7\u53d6\u6570\u636e<\/strong>\u201d\u6309\u94ae\u540e\uff0c\u4fbf\u53bb\u53d6\u5f53\u524d\u78c1\u529b\u8ba1\u7684\u6570\u636e\u5e76\u663e\u793a\u51fa\u6765\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-44-1024x580.png\" alt=\"\" class=\"wp-image-5807\" width=\"461\" height=\"260\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44-1024x580.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44-300x170.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44-768x435.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44-830x470.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44-230x130.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44-350x198.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44-480x272.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-44.png 1240w\" sizes=\"(max-width: 461px) 100vw, 461px\" \/><\/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\u6570\u636e\u4fe1\u606f\n    @IBOutlet weak var textView: UITextView!\n     \n    \/\/\u8fd0\u52a8\u7ba1\u7406\u5668\n    let motionManager = CMMotionManager()\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n    }\n     \n    \/\/\u70b9\u51fb\u83b7\u53d6\u5f53\u524d\u78c1\u529b\u8ba1\u6570\u636e\n    @IBAction func getMagnetometerData(_ sender: Any) {\n        \/\/\u5224\u65ad\u8bbe\u5907\u652f\u6301\u60c5\u51b5\n        guard motionManager.isMagnetometerAvailable else {\n            self.textView.text = \"\\n\u5f53\u524d\u8bbe\u5907\u4e0d\u652f\u6301\u78c1\u529b\u8ba1\\n\"\n            return\n        }\n         \n        \/\/\u83b7\u53d6\u6570\u636e\n        self.motionManager.startMagnetometerUpdates()\n        if let magnetometerData = self.motionManager.magnetometerData {\n            let magneticField = magnetometerData.magneticField\n            var text = \"---\u5f53\u524d\u78c1\u529b\u8ba1\u6570\u636e---\\n\"\n            text += \"x: \\(magneticField.x)\\n\"\n            text += \"y: \\(magneticField.y)\\n\"\n            text += \"z: \\(magneticField.z)\\n\"\n            self.textView.text = text\n        }\n    }\n     \n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/pre>\n\n\n\n<p><strong>2 \u5b9e\u65f6\u91c7\u96c6\u6240\u6709\u6570\u636e<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u6548\u679c\u56fe<br>\u8fd9\u4e2a\u4fbf\u662f\u524d\u9762\u8bb2\u7684\u63a8\u6570\u636e\uff08<strong>push<\/strong>\uff09\u3002\u6839\u636e\u6211\u4eec\u8bbe\u7f6e\u7684\u91c7\u96c6\u9891\u7387\uff08\u901a\u77e5\u9891\u7387\uff09\u6765\u4e0d\u65ad\u5730\u83b7\u53d6\u6700\u65b0\u6570\u636e\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-45-1024x451.png\" alt=\"\" class=\"wp-image-5808\" width=\"584\" height=\"256\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45-1024x451.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45-300x132.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45-768x338.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45-830x365.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45-230x101.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45-350x154.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45-480x211.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-45.png 1240w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/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\u6570\u636e\u4fe1\u606f\n    @IBOutlet weak var textView: UITextView!\n     \n    \/\/\u8fd0\u52a8\u7ba1\u7406\u5668\n    let motionManager = CMMotionManager()\n     \n    \/\/\u5237\u65b0\u65f6\u95f4\u95f4\u9694\n    let timeInterval: TimeInterval = 0.2\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u5f00\u59cb\u78c1\u529b\u8ba1\u66f4\u65b0\n        startMagnetometerUpdates()\n    }\n     \n    \/\/ \u5f00\u59cb\u83b7\u53d6\u78c1\u529b\u8ba1\u6570\u636e\n    func startMagnetometerUpdates() {\n        \/\/\u5224\u65ad\u8bbe\u5907\u652f\u6301\u60c5\u51b5\n        guard motionManager.isMagnetometerAvailable else {\n            self.textView.text = \"\\n\u5f53\u524d\u8bbe\u5907\u4e0d\u652f\u6301\u78c1\u529b\u8ba1\\n\"\n            return\n        }\n         \n        \/\/\u8bbe\u7f6e\u5237\u65b0\u65f6\u95f4\u95f4\u9694\n        self.motionManager.magnetometerUpdateInterval = self.timeInterval\n         \n        \/\/\u5f00\u59cb\u5b9e\u65f6\u83b7\u53d6\u6570\u636e\n        let queue = OperationQueue.current\n        self.motionManager.startMagnetometerUpdates(to: queue!, withHandler: {\n            (magnetometerData, error) in\n            guard error == nil else {\n                print(error!)\n                return\n            }\n            \/\/ \u6709\u66f4\u65b0\n            if self.motionManager.isMagnetometerActive {\n                if let magneticField = magnetometerData?.magneticField {\n                    var text = \"---\u5f53\u524d\u78c1\u529b\u8ba1\u6570\u636e---\\n\"\n                    text += \"x: \\(magneticField.x)\\n\"\n                    text += \"y: \\(magneticField.y)\\n\"\n                    text += \"z: \\(magneticField.z)\\n\"\n                    self.textView.text = text\n                }\n            }\n        })\n    }\n     \n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 CoreMotion \u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u83b7\u53d6\u8bbe\u5907\u5404\u79cd\u4f20\u611f\u5668\u7684\u6570\u636e\uff08\u6bd4\u5982\uff1a\u52a0\u901f\u5668\u3001\u9640\u87ba\u4eea\u3001\u78c1\u529b\u8ba1\uff09\u3002\u4e0a [&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\/5805"}],"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=5805"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5805\/revisions"}],"predecessor-version":[{"id":5809,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5805\/revisions\/5809"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}