TransitionEffect动画循环播放如何关闭

​在自定义方格验证码场景,自定义光标动画创建完以后无法关闭导致同个方框上多个动画执行,影响性能和体验,这个优化需要如何处理呢?尝试过设置iterations为0的情况,会导致出现其他问题

HarmonyOS
2024-01-31 10:13:48
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zhangxiuyong

参考代码

@Entry 
@Component 
export struct SmsPasswordNumberComponent { 
  @State list: Array<string> = ['a', 'b', 'c']; 
  @State myOpacity: number[] = [0, 0, 0]; 
  @State focusIndex: number = 0; 
 
  build() { 
    Row() { 
 
      ForEach(this.list, (item: string, index: number) => { 
        Stack() { 
          Text(item) 
            .fontColor('#181818') 
            .fontSize(30) 
            .fontWeight(FontWeight.Bold) 
            .fontFamily('dipro_medium') 
 
          Divider() 
            .width(1) 
            .backgroundColor('#006AE9') 
            .height(20) 
            .opacity(this.myOpacity[index]) 
            .onAppear(() => { 
              animateTo({ iterations: -1, curve: Curve.Linear }, () => { 
                this.myOpacity[index] = 1; 
              }); 
            }) 
        } 
        .border({ 
          width: 1, 
          color: '#006AE9', 
          radius: 4 
        }) 
        .backgroundColor('#F2F2F2') 
        .alignContent(Alignment.Center) 
        .width(38) 
        .height(52) 
        .margin({ right: index == 2 ? 30 : 10 }) 
        .onClick(() => { 
          this.focusIndex = index; 
          if (this.myOpacity[index] == 1) { 
            // 奇数次点击停掉动画 
            animateTo({ duration: 0 }, () => { 
              // 在duration为0的动画闭包中,改变属性值,停止该属性的动画 
              this.myOpacity[index] = 0; 
            }); 
          } else { 
            // 偶数次点击开启动画 
            animateTo({ iterations: -1, curve: Curve.Linear }, () => { 
              this.myOpacity[index] = 1; 
            }); 
          } 
        }) 
      }) 
 
    } 
 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
分享
微博
QQ
微信
回复
2024-02-01 17:19:27
相关问题
HarmonyOS Image播放gif没有自动循环播放
512浏览 • 1回复 待解决
HarmonyOS 页面跳转如何关闭动画
830浏览 • 1回复 待解决
HarmonyOS 如何播放pag动画
619浏览 • 1回复 待解决
HarmonyOS 本地lottie动画无法播放
1390浏览 • 1回复 待解决
动画lottie能否设置播放次数
2664浏览 • 1回复 待解决
HarmonyOS custondialog显示和关闭动画设置
1637浏览 • 1回复 待解决