美文网首页
小程序—写了一个九宫格缓慢停止转动的效果

小程序—写了一个九宫格缓慢停止转动的效果

作者: 汀上 | 来源:发表于2018-05-02 10:39 被阅读0次

    场景:点击,开始快速旋转(速度可调),当后台中奖下标(下标从零开始顺时针计数)请求下来以后,旋转两圈后停下(圈数可调)

      luckIndexNum: function () {

        let _this = this;  //保存指针

        let indexSelect = 0; //转盘未开始时的默认下标

        let taskExecutionCycle = 1, taskCycleMultiple = 0;   // taskExecutionCycle:旋转周期  ,taskCycleMultiple :旋转计数

        let  luckDrawInfoIndex;

        let num = 0;   // 控制旋转的圈数

        let timer = setInterval(function () {   // 开始定时器

          luckDrawInfoIndex = _this.data.luckyDrawInfoIndex;  //拿到当前请求返回的下标,这个下标应该是全局变量,在请求成功后的回调中同步数据

          console.log(luckDrawInfoIndex)

          if (taskCycleMultiple >= taskExecutionCycle) {  //如果旋转计数大于旋转周期,那么旋转计数清零,周期加一,这样旋转计数从零开始累加,就达到了每周旋转的周期时间的延长,也就是缓慢降速

            taskCycleMultiple = 0;   //清零

            indexSelect++;  //中奖下标下移

            indexSelect = indexSelect % 8;

            _this.setData({

              indexSelect: indexSelect

            })

            if (num <= 14) {  //小于等于14(可控制,这里是匀速转两圈)的时候,匀速转,此时的 taskExecutionCycle(旋转周期)不累加

              taskExecutionCycle = taskExecutionCycle

              num++;

            } else {

              taskExecutionCycle += 1;   //两圈过后开始减速转动

              num++;

              if (num >= 24) {  // 减速再转一圈

                if (indexSelect == luckDrawInfoIndex) {   // 如果当前旋转到的下标等于请求下来的中奖下标

                  console.log(num)

                  clearInterval(timer)  // 清空定时器,停下

                  setTimeout(function () {

                    //获奖提示,这里可以弹一个提示框

                    wx.showModel({

                      title:'恭喜中奖'

                    })

                  }, 1000)

                }

              }

            }

          } else {

            taskCycleMultiple += 1; //旋转计数的累加

          }

        }, 50); //定时器执行的时间间隔(可控)

      },

    相关文章

      网友评论

          本文标题:小程序—写了一个九宫格缓慢停止转动的效果

          本文链接:https://www.haomeiwen.com/subject/gmmqrftx.html