收藏
回答

worklet.spring 给value 赋值为NaN?

initAnimation初始化的时候都正常,到didSpring方法中,this.sharedValueArr[idx].value) = NaN了

把spring换成timing方法就可以正常执行。

console.log("Calling spring with:", this.sharedValueArr[idx].value);


import { worklet } from "../../../utils/animation/worklet-api";
const { shared, runOnUI, runOnJS, repeat, sequence, spring, timing, Easing, cancelAnimation } = worklet;
const { screenWidth } = wx.getSystemInfoSync();

Component()
   .behavior(audioContext)
   .options({ multipleSlots: true })
   .property("safeBottom", { type: Number, value: 0 })
   .property("isSent", { type: Boolean, value: false })
   .property("record", { type: Object, value: {} })
   .data(() => ({
      showMenu: false,
      move: "",
   }))
   .lifetime("attached", function () {})
   .pageLifetime("show", function () {
      this.initAnimation();
   })
   .methods({
      initAnimation() {
         const elements = [
            { id: "menumove", toValue: { x: 0 } },
            { id: "playmove", toValue: { x: 40 } },
         ];
         var _this = this;
         var numsOfBox = elements.length;
         var sharedValueArr = [];

         var _loop = function _loop(i) {
            var translate = shared(elements[i].toValue);
            sharedValueArr.push(translate);
            console.log("translate", translate);
            _this.applyAnimatedStyle(`.${elements[i].id}`, function () {
               "worklet";

               return {
                  transform: `translateX(${translate.value.x}px)`,
               };
            });
         };

         for (var i = 0; i < numsOfBox; i++) {
            _loop(i);
         }

         this.sharedValueArr = sharedValueArr;
         console.log("sharedValueArr", sharedValueArr);
      },

      startAnimation: function startAnimation() {
         const values = [{ x: screenWidth - 90 }, { x: 335 }];
         this.didTiming(0, values[0]);
         this.didSpring(1, values[1]);
      },

      didTiming(idx, toValue) {
         this.sharedValueArr[idx].value = timing(
            toValue,
            {
               duration: 1500,
               easing: Easing.linear,
            },
            function () {
               "worklet";
               console.log("timing done");
            }
         );
      },

      didSpring(idx, toValue) {
         console.log("Calling spring toValue:", toValue);
         this.sharedValueArr[idx].value = spring(
            toValue,
            {
               damping: 20,
               stiffness: 90,
            },
            function () {
               "worklet";
               console.log("spring done");
            }
         );
         console.log("Calling spring with:", this.sharedValueArr[idx].value);
      },
   })
   .register();


回答关注问题邀请回答
收藏

1 个回答

  • 社区技术运营专员-Jahozheng
    社区技术运营专员-Jahozheng
    2024-12-18

    请按照链接提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2024-12-18
    有用
    回复
登录 后发表内容