{"id":1526,"date":"2023-03-25T15:07:26","date_gmt":"2023-03-25T07:07:26","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1526"},"modified":"2023-04-27T21:44:33","modified_gmt":"2023-04-27T13:44:33","slug":"wechat-mini-program-realize-shake-function","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/wechat-mini-program-realize-shake-function\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5b9e\u73b0\u6447\u4e00\u6447\u529f\u80fd"},"content":{"rendered":"<p>\u6447\u4e00\u6447\u4e00\u76f4\u662f\u5fae\u4fe1\u7ecf\u5178\u7684\u5c0f\u529f\u80fd\u3002\u5177\u4f53\u6548\u679c\u4e3a\uff1a\u5982\u679c\u6447\u4e00\u6447\u6210\u529f\uff0c\u90a3\u4e48\u64ad\u653e\u4e00\u6bb5\u97f3\u9891\u3002<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u6b64\u529f\u80fd\u9700\u8981\u771f\u673a\u652f\u6301\uff0c\u5728\u6a21\u62df\u5668\u4e2d\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c<\/p>\n<\/blockquote>\n<p><!-- more --><\/p>\n<p>\u6447\u4e00\u6447\u7684\u5177\u4f53\u5b9e\u73b0\u539f\u7406\u4e3a\uff1a\u5229\u7528\u624b\u673a\u7684\u91cd\u529b\u611f\u5e94\u673a\u5236\u76d1\u542c\u624b\u673a\u6447\u6643\u5e45\u5ea6\u3002\u5982\u679c\u624b\u673a\u6447\u6643\u5e45\u5ea6\u8d85\u8fc7\u4e00\u5b9a\u504f\u79fb\u91cf\u5c31\u8ba4\u4e3a\u6447\u4e00\u6447\u6210\u529f\uff0c\u53ef\u4ee5\u6267\u884c\u5177\u4f53\u4e1a\u52a1\uff1b\u5982\u679c\u624b\u673a\u6447\u6643\u5e45\u5ea6\u4e0d\u5927\u5c31\u8ba4\u4e3a\u4e0d\u662f\u6447\u4e00\u6447\u3002MINA\u6846\u67b6\u63d0\u4f9b\u7684\u91cd\u529b\u611f\u5e94\u76d1\u542c\u51fd\u6570\u4e3awx.onAccelerometerChange(CALLBACK)\u3002CALLBACK\u56de\u8c03\u51fd\u6570\u4e2d\u5c06\u53ef\u4ee5\u83b7\u5f97\u91cd\u529b\u611f\u5e94\u5728x\u3001y\u3001z\u8f74\u4e0a\u7684\u503c\u3002<\/p>\n<h2>\u81ea\u5b9a\u4e49Shake\u6a21\u677f<\/h2>\n<p>\/templates\/index.js<\/p>\n<pre><code class=\"language-javascript\">exports.Shake = require(&#039;.\/shake\/shake&#039;)<\/code><\/pre>\n<p>\/templates\/index.wxss<\/p>\n<pre><code class=\"language-css\">@import &quot;shake\/shake.wxss&quot;;<\/code><\/pre>\n<p>\/templates\/shake\/shake.wxml<\/p>\n<pre><code class=\"language-xml\">&lt;template name=&quot;shake&quot;&gt;\n  &lt;modal title=&#039;\u6447\u4e00\u6447&#039; confirm-text=&#039;\u786e\u5b9a&#039; no-cancel hidden=&#039;{{shakeInfo.gravityModalHidden}}&#039; bindconfirm=&#039;shake&#039; confirm-color=&#039;#1f4ba5&#039;&gt;\n    \u6447\u4e00\u6447\uff0c\u62a2\u4f1a\u5458\u5361\n  &lt;\/modal&gt;\n&lt;\/template&gt;<\/code><\/pre>\n<p>\/templates\/shake\/shake.js<\/p>\n<pre><code class=\"language-javascript\">\/**\n * \u6447\u4e00\u6447\n *\/\nvar Shake = {\n  shake() {\n    var that = this;\n    this.gravityModalConfirm(true);\n    wx.startAccelerometer()\n    wx.onAccelerometerChange(function (res) {\n      var x = res.x.toFixed(4),\n        y = res.y.toFixed(4),\n        z = res.z.toFixed(4);\n      var flagX = that.getDelFlag(x, that.data.shakeData.x),\n        flagY = that.getDelFlag(y, that.data.shakeData.y),\n        flagZ = that.getDelFlag(z, that.data.shakeData.z);\n      var shakeData = { x: x, y: y, z: z };\n      that.setData({ shakeData: shakeData });\n      if ((flagX &amp;&amp; flagY) || (flagX &amp;&amp; flagZ) || (flagY &amp;&amp; flagZ)) {\n        \/\/\u5e45\u5ea6\u8db3\u591f\u5927\uff0c\u6447\u4e00\u6447\u6210\u529f\n        if (that.data.shakeInfo.enable) {\n          that.data.shakeInfo.enable = false;\n          \/\/wx.stopAccelerometer()\n          that.shakeSuccess();\n        }\n      }\n    });\n  },\n\n  \/\/\u542f\u7528\u6216\u505c\u7528\u6447\u4e00\u6447\u529f\u80fd\n  gravityModalConfirm(flag) {\n    if (flag !== true) {\n      flag = false;\n    }\n    var shakeInfo = { gravityModalHidden: !this.data.shakeInfo.gravityModalHidden, enable: flag };\n    this.setData({ shakeInfo: shakeInfo });\n  },\n\n  \/\/\u8ba1\u7b97\u6447\u4e00\u6447\u7684\u504f\u79fb\u91cf\n  getDelFlag(val1, val2) {\n    return (Math.abs(val1 - val2) &gt;= 1);\n  },\n}\n\nmodule.exports = Shake;<\/code><\/pre>\n<h2>\u5f15\u7528Shake\u6a21\u677f<\/h2>\n<p>\/pages\/index\/index.wxml<\/p>\n<pre><code class=\"language-xml\">&lt;import src=&quot;\/templates\/shake\/shake.wxml&quot; \/&gt;\n\n&lt;view class=&#039;container&#039;&gt;\n  &lt;template is=&quot;shake&quot; data=&#039;{{shakeInfo}}&#039; \/&gt;\n&lt;\/view&gt;<\/code><\/pre>\n<p>\/pages\/index\/index.js<\/p>\n<pre><code class=\"language-javascript\">\/\/index.js\n\/\/\u83b7\u53d6\u5e94\u7528\u5b9e\u4f8b\nconst app = getApp()\nvar Zan = require(&#039;..\/..\/templates\/index&#039;);\n\nPage(Object.assign({\n  data: {\n    shakeInfo: { gravityModalHidden: false, enable: true },\n    shakeData: { x: 0, y: 0, z: 0 },\n  },\n\n  onLoad: function () {\n\n  },\n}, Zan.Shake, {\n  \/\/\u6447\u4e00\u6447\u6210\u529f\u540e\u7684\u64cd\u4f5c\n  shakeSuccess() {\n    console.log(&#039;Shake Success&#039;)\n    var that = this;\n    wx.playBackgroundAudio({\n      dataUrl: &#039;http:\/\/www.yezhou.me\/audio\/wx_app_shake.mp3&#039;,\n\n    })\n\n    wx.onBackgroundAudioStop(function () {\n      wx.showToast({\n        title: &#039;\u6416\u4e00\u6416\u6210\u529f&#039;,\n      });\n      var shakeinfo = that.data.shakeInfo;\n      shakeinfo = { enable: true, gravityModalHidden: false };\n      that.setData({ shakeInfo: shakeinfo });\n    })\n  },\n}))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6447\u4e00\u6447\u4e00\u76f4\u662f\u5fae\u4fe1\u7ecf\u5178\u7684\u5c0f\u529f\u80fd\u3002\u5177\u4f53\u6548\u679c\u4e3a\uff1a\u5982\u679c\u6447\u4e00\u6447\u6210\u529f\uff0c\u90a3\u4e48\u64ad\u653e\u4e00\u6bb5\u97f3\u9891\u3002 \u6ce8\u610f\uff1a\u6b64\u529f\u80fd\u9700\u8981\u771f\u673a\u652f\u6301\uff0c\u5728\u6a21\u62df [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-1526","post","type-post","status-publish","format-standard","hentry","category-wechat-mini-program"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1526"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1526\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}