{"id":5810,"date":"2022-05-11T13:54:25","date_gmt":"2022-05-11T05:54:25","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5810"},"modified":"2022-05-11T13:54:25","modified_gmt":"2022-05-11T05:54:25","slug":"ios%e4%bc%a0%e6%84%9f%e5%99%a8%ef%bc%9a%e4%bd%bf%e7%94%a8%e9%99%80%e8%9e%ba%e4%bb%aa%e5%ae%8c%e6%88%90%e4%b8%80%e4%b8%aa%e5%b0%8f%e7%90%83%e6%92%9e%e5%a3%81%e7%9a%84%e5%b0%8f%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5810","title":{"rendered":"iOS\u4f20\u611f\u5668\uff1a\u4f7f\u7528\u9640\u87ba\u4eea\u5b8c\u6210\u4e00\u4e2a\u5c0f\u7403\u649e\u58c1\u7684\u5c0f\u6e38\u620f"},"content":{"rendered":"\n<p>\u54b1\u4eec\u4f1a\u5b8c\u6210\u4e00\u4e2a\u5c0f\u7403\u649e\u58c1\u7684\u5c0f\u6e38\u620f\u3002\u5c0f\u7403\u53ef\u4ee5\u611f\u53d7\u5230\u91cd\u529b\uff0c\u4ece\u800c\u80fd\u591f\u968f\u7740\u624b\u673a\u7684\u8fd0\u52a8\u6765\u4e00\u8d77\u8fd0\u52a8\u3002\u4e3a\u4e86\u589e\u52a0\u4e00\u70b9\u70b9\u8da3\u5473\u6027\uff0c\u5bf9\u5c0f\u7403\u7684\u8fd0\u52a8\u8303\u56f4\u505a\u4e86\u9650\u5236\u3002\u5f53\u5c0f\u7403\u78b0\u5230\u5c4f\u5e55\u7684\u8fb9\u7f18\u7684\u65f6\u5019\uff0c\u4f1a\u8fdb\u884c\u53cd\u5f39\uff0c\u76f8\u53cd\u65b9\u5411\u8fd0\u52a8\u3002\u5176\u5b9e\u4e0d\u7ba1\u662f\u52a0\u901f\u8ba1\u8fd8\u662f\u4eca\u5929\u7684\u9640\u87ba\u4eea\uff0c\u90fd\u662f\u7528\u5230\u4e86\u4e0a\u6b21\u8bf4\u7684iOS\u5f53\u4e2d\u7684\u90a3\u4e2a\u6838\u5fc3\u8fd0\u52a8\u6846\u67b6<code>CoreMotion<\/code>\u3002<\/p>\n\n\n\n<p><strong>1. \u9640\u87ba\u4eea\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9640\u87ba\u4eea\u4e3b\u8981\u662f\u7528\u6765\u6d4b\u91cf\u6cbf\u7740\u67d0\u4e2a\u7279\u5b9a\u7684\u5750\u6807\u8f74\u65cb\u8f6c\u901f\u5ea6\u7684\u3002\u5728\u4f7f\u7528\u4e2d\uff0c\u9640\u87ba\u4eea\u59cb\u7ec8\u6307\u5411\u4e00\u4e2a\u56fa\u5b9a\u7684\u65b9\u5411\uff0c\u5f53\u8fd0\u52a8\u7269\u4f53\u7684\u8fd0\u52a8\u65b9\u5411\u504f\u79bb\u9884\u5b9a\u65b9\u5411\u65f6\uff0c\u9640\u87ba\u4eea\u5c31\u53ef\u4ee5\u611f\u53d7\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u5728\u624b\u673a\u4e0a\uff0c\u4ec5\u7528\u52a0\u901f\u5ea6\u8ba1\u6ca1\u529e\u6cd5\u6d4b\u91cf\u6216\u91cd\u6784\u51fa\u5b8c\u6574\u76843D\u52a8\u4f5c\uff0c\u6d4b\u4e0d\u5230\u8f6c\u52a8\u7684\u52a8\u4f5c\u7684\uff0c\u52a0\u901f\u8ba1\u53ea\u80fd\u68c0\u6d4b\u8f74\u5411\u7684\u7ebf\u6027\u52a8\u4f5c\u3002\u4f46\u9640\u87ba\u4eea\u5219\u53ef\u4ee5\u5bf9\u8f6c\u52a8\u3001\u504f\u8f6c\u7684\u52a8\u4f5c\u505a\u5f88\u597d\u7684\u6d4b\u91cf\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u7cbe\u786e\u5206\u6790\u5224\u65ad\u51fa\u4f7f\u7528\u8005\u7684\u5b9e\u9645\u52a8\u4f5c\u3002\u800c\u540e\u6839\u636e\u52a8\u4f5c\uff0c\u53ef\u4ee5\u5bf9\u624b\u673a\u505a\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>1.1 \u9640\u87ba\u4eea\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<p>\u5404\u4f4d\u7ae5\u978b\u76f8\u6bd4\u90fd\u73a9\u8fc7Wii\uff0c\u90a3\u4e2a\u4f53\u611f\u624b\u67c4\u80af\u5b9a\u5c31\u7528\u5230\u4e86\u9640\u87ba\u4eea\u3002\u73a9\u5bb6\u901a\u8fc7\u6325\u52a8\u8fd0\u52a8\u624b\u67c4\uff0c\u6765\u63a7\u5236\u6e38\u620f\u3002\u4f8b\u5982\u4e52\u4e53\u7403\u3001\u7f51\u7403\u3001\u8d5b\u8f66\u7b49\u7b49\u3002\u6709\u4e00\u4e9b\u9177\u70ab\u7684APP\u4f1a\u901a\u8fc7\u5c0f\u5e45\u5ea6\u7684\u503e\u659c\uff0c\u504f\u8f6c\u624b\u673a\uff0c\u5b9e\u73b0\u5f69\u86cb\u529f\u80fd\uff0c\u4f8b\u5982\u653e\u5927\u7f29\u5c0f\u4e4b\u7c7b\u7684\u3002\u6216\u8005\u628a\u624b\u673a\u5c4f\u5e55\u7ffb\u8f6c\uff0c\u5c31\u53ef\u4ee5\u62d2\u63a5\u7535\u8bdd\u6216\u8005\u9759\u97f3\u5565\u7684\u3002<br>\u62cd\u7167\u7c7b\u7684APP\u4e5f\u4f1a\u901a\u8fc7\u9640\u87ba\u4eea\u628a\u62cd\u7167\u65f6\u5019\u624b\u7684\u6296\u52a8\u53cd\u9988\u4ea4\u7ed9\u56fe\u50cf\u5904\u7406\u5668\uff0c\u4ee5\u4fbf\u6293\u5230\u66f4\u6e05\u6670\u7a33\u5b9a\u7684\u56fe\u7247\u3002<\/p>\n\n\n\n<p><strong>1.2 \u9640\u87ba\u4eea\u5728iOS\u4e2d\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p>iPhone\u3001iPad\u3001iWatch\u90fd\u6709\u5185\u7f6e\u7684\u9640\u87ba\u4eea\uff0c\u4e5f\u90fd\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u8fdb\u884c\u8c03\u7528\u3002\u540c\u6837\uff0c\u7528\u4e00\u5f20\u56fe\u5c55\u73b0\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-46.png\" alt=\"\" class=\"wp-image-5811\" width=\"423\" height=\"416\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-46.png 700w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-46-300x296.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-46-230x227.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-46-350x345.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-46-480x473.png 480w\" sizes=\"(max-width: 423px) 100vw, 423px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2. \u9640\u87ba\u4eea\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2.1 \u4f7f\u7528\u6b65\u9aa4<\/strong><\/p>\n\n\n\n<p>\u9640\u87ba\u4eea\u540c\u6837\u4e5f\u662f\u901a\u8fc7<code>CoreMotion<\/code>\u8fd9\u4e2a\u6846\u67b6\u6765\u7ba1\u7406\u7684\uff0c\u6240\u4ee5\u548c\u52a0\u901f\u8ba1\u4e00\u6837\uff0c\u56db\u4e2a\u6807\u51c6\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ul><li>\u521d\u59cb\u5316CMMotionManager\u7ba1\u7406\u5bf9\u8c61\uff1b<\/li><li>\u8c03\u7528\u7ba1\u7406\u5bf9\u8c61\u7684\u5bf9\u8c61\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\uff1b<\/li><li>\u5904\u7406\u6570\u636e\uff1b<\/li><li>\u5f53\u4e0d\u9700\u8981\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u505c\u6b62\u83b7\u53d6\u6570\u636e\u3002<\/li><\/ul>\n\n\n\n<p><strong>2.2 \u9640\u87ba\u4eea\u6570\u636e\u83b7\u53d6\u7684\u4e24\u79cd\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>CoreMotion\u4e2d\u67092\u79cd\u83b7\u53d6\u6570\u636e\u65b9\u5f0f\uff0c\u4e00\u79cd\u53eb\u505aPUSH\u7684\u65b9\u5f0f\uff0c\u4e00\u79cd\u53eb\u505aPULL\u7684\u65b9\u5f0f\u3002\u987e\u540d\u601d\u4e49\uff0cPUSH\u5c31\u662f\u88ab\u52a8\u7684\u83b7\u53d6\u3002\u8bbe\u5b9a\u5b8c\u4e86\u4e4b\u540e\uff0c\u7ebf\u7a0b\u5b9a\u65f6\u628a\u83b7\u53d6\u5230\u7684\u6570\u636e\u63a8\u9001\u56de\u6765\u3002\u53ef\u60f3\u800c\u77e5\uff0c\u5bf9\u4e8e\u8d44\u6e90\u7684\u6d88\u8017\u662f\u4f1a\u7a0d\u5fae\u5927\u4e00\u70b9\u7684\u3002<br>PULL\uff0c\u5c31\u662f\u8981\u53bb\u7d22\u53d6\u3002\u62c9\u4e00\u4e0b\u624d\u4f1a\u83b7\u53d6\u5230\u6570\u636e\u3002\u4e0d\u8981\u4e0d\u7ed9\u3002\u4e0a\u4e00\u6b21\u52a0\u901f\u8ba1\u54b1\u4eec\u7ed9\u51fa\u7684\u4ee3\u7801\u662fOC\u7684\uff0c\u4eca\u5929\u54b1\u4eec\u5c31\u7528Swift\u7684\u3002<\/p>\n\n\n\n<p><strong>2.2.1 PULL\u7684\u65b9\u5f0f<\/strong><\/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=\"\"> private func useGyroPull() {\n        \/\/\u5224\u65ad\u9640\u87ba\u4eea\u53ef\u4e0d\u53ef\u7528\n        if manager.isGyroAvailable {\n            \/\/\u8bbe\u7f6e\u9640\u87ba\u4eea\u591a\u4e45\u91c7\u6837\u4e00\u6b21\n            manager.gyroUpdateInterval = 0.1\n            \/\/\u5f00\u59cb\u66f4\u65b0\uff0c\u540e\u53f0\u7ebf\u7a0b\u5f00\u59cb\u8fd0\u884c\u3002\u8fd9\u662fPull\u65b9\u5f0f\u3002\n            manager.startGyroUpdates()\n            \n        }\n        \/\/\u83b7\u53d6\u5e76\u5904\u7406\u9640\u87ba\u4eea\u6570\u636e\u3002\u8fd9\u91cc\u6211\u4eec\u5c31\u53ea\u662f\u7b80\u5355\u7684\u505a\u4e86\u6253\u5370\u3002\n        print(\"X = \\(manager.gyroData?.rotationRate.x ?? 0)\",\"Y = \\(manager.gyroData?.rotationRate.y ?? 0)\",\"Z = \\(manager.gyroData?.rotationRate.z ?? 0)\")\n\n    }<\/pre>\n\n\n\n<p><strong>2.2.2 PUSH\u7684\u65b9\u5f0f<\/strong><\/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=\"\"> private func useGyroPush() {\n        \/\/\u5224\u65ad\u9640\u87ba\u4eea\u53ef\u4e0d\u53ef\u7528\n        if manager.isGyroAvailable {\n            \/\/\u8bbe\u7f6e\u9640\u87ba\u4eea\u591a\u4e45\u91c7\u6837\u4e00\u6b21\n            manager.gyroUpdateInterval = 0.1\n            \/\/Push\u65b9\u5f0f\u83b7\u53d6\u548c\u5904\u7406\u6570\u636e\uff0c\u8fd9\u91cc\u6211\u4eec\u4e00\u6837\u53ea\u662f\u505a\u4e86\u7b80\u5355\u7684\u6253\u5370\u3002\u628a\u91c7\u6837\u7684\u5de5\u4f5c\u653e\u5728\u4e86\u4e3b\u7ebf\u7a0b\u4e2d\u3002\n            manager.startGyroUpdates(to: OperationQueue.main, withHandler: { (gyroData, error) in\n                print(\"X = \\(self.manager.gyroData?.rotationRate.x ?? 0)\",\"Y = \\(self.manager.gyroData?.rotationRate.y ?? 0)\",\"Z = \\(self.manager.gyroData?.rotationRate.z ?? 0)\")\n                \n            })\n        } else {\n            print(\"\u9640\u87ba\u4eea\u4e0d\u53ef\u7528\")\n        }\n\n    }<\/pre>\n\n\n\n<p><strong>3. \u5f00\u59cb\u6211\u4eec\u7684\u5c0f\u6e38\u620f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>3.1 \u601d\u7ef4\u5bfc\u56fe<\/strong><\/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-47-1024x610.png\" alt=\"\" class=\"wp-image-5812\" width=\"521\" height=\"310\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47-1024x610.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47-300x179.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47-768x457.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47-830x494.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47-350x208.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47-480x286.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-47.png 1068w\" sizes=\"(max-width: 521px) 100vw, 521px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3.2 \u5b9e\u73b0<\/strong><\/p>\n\n\n\n<p>3.2.1 \u4ee5X\u8f74\u8fb9\u754c\u503c\u5904\u7406\u53ca\u78b0\u58c1\u540e\u901f\u5ea6\u5904\u7406\u4e3a\u4f8b<\/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=\"\">\/\/            \u5bf9\u7403\u5728X\u8f74\u78b0\u58c1\u8fdb\u884c\u5904\u7406\n            if currentPoint.x &lt;=  imageWidth \/ 2 {\n              currentPoint.x = imageWidth \/ 2\n                ballXVelocity = -ballXVelocity * 0.8\n            }\n            \n            if currentPoint.x >= bounds.size.width - imageWidth \/ 2 {\n                currentPoint.x = bounds.size.width - imageWidth \/ 2\n                ballXVelocity = -ballXVelocity * 0.8\n\n            }\n<\/pre>\n\n\n\n<p>3.2.2 \u5f00\u542f\u9640\u87ba\u4eea\u5e76\u66f4\u65b0<\/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=\"\">  manager.deviceMotionUpdateInterval = 1 \/ 60\n        \/\/\u6ce8\u610f\u4e00\u4e0b\uff0c\u5728Swift\u6ca1\u6709\u4e86NSOperation\u3002\u88abOperationQueue\u53d6\u4ee3\u4e86\u3002\n        manager.startDeviceMotionUpdates(to: OperationQueue.main) { (motion, error) in\n            \n            self.ballView!.accelleration = (motion?.gravity)!\n            \/\/\u5f00\u542f\u4e3b\u961f\u5217\u5f02\u6b65\u7ebf\u7a0b\uff0c\u66f4\u65b0\u7403\u7684\u4f4d\u7f6e\u3002\n            DispatchQueue.main.async {\n                self.ballView!.updateLocation(multiplier: 5000)\n\n            }<\/pre>\n\n\n\n<p>3.2.3 \u66f4\u65b0\u5c0f\u7403\u7684\u4f4d\u7f6e<\/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=\"\">func updateLocation(multiplier : Double) {\n        if (lastUpdateTime != nil) {\n            let updatePeriod : Double = Date.init().timeIntervalSince(lastUpdateTime!)\n            \n            ballXVelocity = ballXVelocity + accelleration.x * updatePeriod\n            ballYVelocity = ballYVelocity + accelleration.y * updatePeriod\n            \n            let coefficient = updatePeriod * multiplier\n            currentPoint = CGPoint(x: currentPoint.x + (CGFloat)(ballXVelocity * coefficient), y: currentPoint.y - (CGFloat)(ballYVelocity * coefficient))\n        }\n        lastUpdateTime = Date()\n\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u54b1\u4eec\u4f1a\u5b8c\u6210\u4e00\u4e2a\u5c0f\u7403\u649e\u58c1\u7684\u5c0f\u6e38\u620f\u3002\u5c0f\u7403\u53ef\u4ee5\u611f\u53d7\u5230\u91cd\u529b\uff0c\u4ece\u800c\u80fd\u591f\u968f\u7740\u624b\u673a\u7684\u8fd0\u52a8\u6765\u4e00\u8d77\u8fd0\u52a8\u3002\u4e3a\u4e86\u589e\u52a0\u4e00\u70b9\u70b9\u8da3\u5473\u6027\uff0c\u5bf9 [&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\/5810"}],"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=5810"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5810\/revisions"}],"predecessor-version":[{"id":5813,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5810\/revisions\/5813"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}