{"id":5822,"date":"2022-05-11T14:19:27","date_gmt":"2022-05-11T06:19:27","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5822"},"modified":"2022-05-11T14:19:27","modified_gmt":"2022-05-11T06:19:27","slug":"swift-%e5%8a%a0%e9%80%9f%e4%bc%a0%e6%84%9f%e5%99%a8%ef%bc%88coremotion%ef%bc%89%e7%9a%84%e7%94%a8%e6%b3%95%ef%bc%8c%e5%b0%8f%e7%90%83%e5%8a%a0%e9%80%9f%e8%bf%90%e5%8a%a8%e5%b9%b6%e5%8f%8d%e5%bc%b9","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5822","title":{"rendered":"Swift &#8211; \u52a0\u901f\u4f20\u611f\u5668\uff08CoreMotion\uff09\u7684\u7528\u6cd5\uff0c\u5c0f\u7403\u52a0\u901f\u8fd0\u52a8\u5e76\u53cd\u5f39\u6837\u4f8b"},"content":{"rendered":"\n<p><strong>1 \u52a0\u901f\u4f20\u611f\u5668\u53ef\u4ee5\u76d1\u542c\u5230x,y,z\u4e09\u4e2a\u65b9\u5411\u7684\u52a0\u901f\u5ea6\uff0c\u4f7f\u7528\u6b65\u9aa4\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u5b9e\u4f8b\u5316CMMotionManager\u7c7b <\/li><li>\u5411CMMotionManager\u7684accelerometerUpdateInterval\u5c5e\u6027\u4e2d\u8bbe\u7f6e\u901a\u77e5\u95f4\u9694\u65f6\u95f4\u503c\u3002<\/li><li>\u4f7f\u7528OperationQueue.current\u5efa\u7acb\u4e00\u4e2a\u76d1\u542c\u961f\u5217\u3002<\/li><li>\u4f7f\u7528startAccelerometerUpdates\u65b9\u6cd5\u66f4\u65b0\u76d1\u542c\u961f\u5217\uff0c\u5e76\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u7528\u4e8e\u63a5\u53d7\u52a0\u901f\u5ea6\u901a\u77e5\u3002\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u4f7f\u7528accelerometerData.acceleration\u76f8\u5173\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6x\u3001y\u3001z\u5404\u4e2a\u65b9\u5411\u7684\u52a0\u901f\u5ea6\u3002<br><\/li><\/ul>\n\n\n\n<p><strong>2 \u901a\u77e5\u9891\u7387\u8bbe\u7f6e\u5efa\u8bae<\/strong><\/p>\n\n\n\n<p>accelerometerUpdateInterval\u8868\u793a\u901a\u77e5\u9891\u7387\uff0c\u8868\u793a\u95f4\u9694\u591a\u5c11\u79d2\u901a\u77e5\u4e00\u6b21\u3002iPhone\u5f00\u53d1\u6587\u6863\u4e2d\u63a8\u8350\u4f7f\u7528\u7684\u901a\u77e5\u95f4\u9694\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>\u68c0\u6d4b\u8bbe\u5907\u671d\u5411\uff1a1\/10 ~ 1\/20<\/li><li>\u5728\u6e38\u620f\u4e2d\u9700\u8981\u5b9e\u65f6\u4f7f\u7528\u52a0\u901f\u4f20\u611f\u5668\u65f6\uff1a1\/30 ~ 1\/60<\/li><li>\u68c0\u6d4b\u6572\u51fb\u8bbe\u5907\u6216\u8005\u5267\u70c8\u6447\u52a8\u8bbe\u5907\u7684\u60c5\u51b5\u4e0b\uff1a1\/70 ~ 1\/100<\/li><\/ul>\n\n\n\n<p><strong>3 x,y,z\u8f74<\/strong><\/p>\n\n\n\n<ul><li>\u5bf9\u4e8eiphone\u624b\u673a\u6765\u8bf4\uff0c\u753b\u9762\u4e0a\u4e0b\u4e3ay\u8f74\uff0c\u5de6\u53f3\u4e3ax\u8f74\uff0c\u8d2f\u7a7f\u5c4f\u5e55\u4e3az\u8f74\u3002<\/li><li>\u5411\u4e0a\uff0c\u5411\u53f3\uff0c\u624b\u673a\u7684\u524d\u9762\u5206\u522b\u662f\u5404\u8f74\u7684\u6b63\u65b9\u5411\u3002<\/li><\/ul>\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-52-1024x672.png\" alt=\"\" class=\"wp-image-5823\" width=\"501\" height=\"329\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52-1024x672.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52-300x197.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52-768x504.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52-830x545.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52-230x151.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52-350x230.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52-480x315.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-52.png 1036w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4 \u52a0\u901f\u5ea6\uff08\u539f\u59cb\u52a0\u901f\u5ea6\uff09<\/strong><\/p>\n\n\n\n<p>\u52a0\u901f\u5ea6\u4e0d\u4ec5\u53d7\u9707\u52a8\u624b\u673a\u65f6\u65bd\u52a0\u7684\u4f5c\u7528\u529b\u7684\u5f71\u54cd\uff0c\u8fd8\u4f1a\u6301\u7eed\u53d7\u5230\u91cd\u529b\u7684\u5f71\u54cd\u3002\u56e0\u6b64iphone\u624b\u673a\u5982\u679c\u5782\u76f4\u62ff\u5728\u624b\u4e0a\u7684\u8bdd\uff0cY\u8f74\u8d1f\u65b9\u5411\u5c06\u53d7\u91cd\u529b\u4f5c\u7528\uff0c\u52a0\u901f\u5ea6y\u5c5e\u6027\u5c06\u4e00\u76f4\u4e3a\u8d1f\u503c\uff08\u6700\u5c0f\u503c\u4e3a-1.0\uff09<\/p>\n\n\n\n<p><strong>5 Gravity\u548cUserAcceleration<\/strong><\/p>\n\n\n\n<p>\u7f51\u53cbcruise_H\u95ee\uff1amotionManager.deviceMotion.userAcceleration.x\u548cmotionManager.accelerometerData!.acceleration.x\u4e24\u4e2a\u83b7\u53d6\u7684acceleration\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/p>\n\n\n\n<p>\u4e0a\u9762\u63d0\u5230\u7684\u539f\u59cb\u7684\u52a0\u901f\u5ea6\uff08\u5373\u901a\u8fc7startAccelerometerUpdates\u83b7\u53d6\u7684\u90a3\u4e2a\u503c\uff09\u5b9e\u9645\u4e0a\u662f\u7531\u4e24\u79cd\u52a0\u901f\u5ea6\u5408\u6210\u800c\u6765\u7684\u3002\u4e00\u4e2a\u662f\u91cd\u529b\u52a0\u901f\u5ea6\uff08Gravity\uff09\uff0c\u4e00\u4e2a\u662f\u7528\u6237\u5bf9\u624b\u673a\u65bd\u52a0\u7684\u52a0\u901f\u5ea6\uff08UserAcceleration\uff09\uff08\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u8fd9\u4e24\u79cd\u52a0\u901f\u5ea6\uff09\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u5f53\u624b\u673a\u5782\u76f4\u9759\u6b62\u65f6\uff0c\u867d\u7136UserAcceleration\u662f0\uff0c\u4f46\u7531\u4e8e\u6709\u91cd\u529b\u52a0\u901f\u5ea6\uff0c\u6240\u4ee5\u4e24\u4e2a\u5408\u6210\u540e\u52a0\u901f\u5ea6y\u5c5e\u6027\u4fbf\u662f\u4e3a\u8d1f\u503c\u3002<\/p>\n\n\n\n<p><strong>6 \u6d4b\u8bd5\u6837\u4f8b<\/strong><\/p>\n\n\n\n<p>\u8be5\u5e94\u7528\u8fd0\u884c\u540e\uff0c\u5c06\u5728\u753b\u9762\u6b63\u4e2d\u592e\u663e\u793a\u4e00\u4e2a\u7403\u4f53\u3002\u503e\u659c\u624b\u673a\u65f6\uff0c\u7403\u4f53\u5c06\u5411\u7740\u503e\u659c\u7684\u65b9\u5411\u8fd0\u52a8\u3002\u78b0\u649e\u5230\u56db\u58c1\u540e\u53cd\u5f39\u56de\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-53-1024x538.png\" alt=\"\" class=\"wp-image-5824\" width=\"520\" height=\"273\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53-1024x538.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53-300x158.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53-768x404.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53-830x436.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53-230x121.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53-350x184.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53-480x252.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-53.png 1130w\" sizes=\"(max-width: 520px) 100vw, 520px\" \/><\/figure><\/div>\n\n\n\n<p>\uff08\u6ce8\u610f\uff1a\u7531\u4e8e\u9700\u8981\u7528\u5230\u8bbe\u5907\u7684\u91cd\u529b\u611f\u5e94\u5668\uff0c\u6240\u4ee5\u8981\u4f7f\u7528\u771f\u673a\u8c03\u8bd5\uff0c\u6a21\u62df\u5668\u8fd0\u884c\u5c0f\u7403\u4e0d\u4f1a\u52a8\u3002\uff09<\/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,UIAccelerometerDelegate {\n     \n    var ball:UIImageView!\n    var speedX:UIAccelerationValue = 0\n    var speedY:UIAccelerationValue = 0\n    var motionManager = CMMotionManager()\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \/\/\u653e\u4e00\u4e2a\u5c0f\u7403\u5728\u4e2d\u592e\n        ball = UIImageView(image:UIImage(named:\"ball\"))\n        ball.frame = CGRect(x:0, y:0, width:50, height:50)\n        ball.center = self.view.center\n        self.view.addSubview(ball)\n         \n        motionManager.accelerometerUpdateInterval = 1\/60\n         \n        if motionManager.isAccelerometerAvailable {\n            let queue = OperationQueue.current\n            motionManager.startAccelerometerUpdates(to: queue!, withHandler: {\n                (accelerometerData, error) in\n                    \/\/\u52a8\u6001\u8bbe\u7f6e\u5c0f\u7403\u4f4d\u7f6e\n                    self.speedX += accelerometerData!.acceleration.x\n                    self.speedY +=  accelerometerData!.acceleration.y\n                    var posX=self.ball.center.x + CGFloat(self.speedX)\n                    var posY=self.ball.center.y - CGFloat(self.speedY)\n                    \/\/\u78b0\u5230\u8fb9\u6846\u540e\u7684\u53cd\u5f39\u5904\u7406\n                    if posX&lt;0 {\n                        posX=0;\n                        \/\/\u78b0\u5230\u5de6\u8fb9\u7684\u8fb9\u6846\u540e\u4ee50.4\u500d\u7684\u901f\u5ea6\u53cd\u5f39\n                        self.speedX *= -0.4\n                         \n                    }else if posX > self.view.bounds.size.width {\n                        posX=self.view.bounds.size.width\n                        \/\/\u78b0\u5230\u53f3\u8fb9\u7684\u8fb9\u6846\u540e\u4ee50.4\u500d\u7684\u901f\u5ea6\u53cd\u5f39\n                        self.speedX *= -0.4\n                    }\n                    if posY&lt;0 {\n                        posY=0\n                        \/\/\u78b0\u5230\u4e0a\u9762\u7684\u8fb9\u6846\u4e0d\u53cd\u5f39\n                        self.speedY=0\n                    } else if posY>self.view.bounds.size.height{\n                        posY=self.view.bounds.size.height\n                        \/\/\u78b0\u5230\u4e0b\u9762\u7684\u8fb9\u6846\u4ee51.5\u500d\u7684\u901f\u5ea6\u53cd\u5f39\n                        self.speedY *= -1.5\n                    }\n                self.ball.center = CGPoint(x:posX, y:posY)\n            })\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1 \u52a0\u901f\u4f20\u611f\u5668\u53ef\u4ee5\u76d1\u542c\u5230x,y,z\u4e09\u4e2a\u65b9\u5411\u7684\u52a0\u901f\u5ea6\uff0c\u4f7f\u7528\u6b65\u9aa4\u5982\u4e0b\uff1a \u5b9e\u4f8b\u5316CMMotionManager\u7c7b \u5411 [&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\/5822"}],"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=5822"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5822\/revisions"}],"predecessor-version":[{"id":5825,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5822\/revisions\/5825"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}