HarmonyOS SideBarContainer不用自带的ControlButton,自己控制showSideBar的话,没动画效果?

使用自己的按钮控制SideBarContainer,展开关闭没动画效果,也不支持滑动关闭

阅读 531
1 个回答

目前的替代方案是需要自定义动画效果

请参考demo

@Entry
@Component
struct Index {
  @State arr: number[] = [1, 2, 3]
  @State current: number = 1
  @State showSideBar: boolean = false

  @State showFlag: Visibility = Visibility.Visible;

  // 延迟关闭弹窗,让自定义的出场动画显示
  cancel(){
    this.showFlag=Visibility.Hidden
    setTimeout(()=>{
      this.showSideBar = !this.showSideBar
    }, 1000)
  }

  build() {
    SideBarContainer(SideBarContainerType.Overlay) {
      Column() {
        ForEach(this.arr, (item: number) => {
          Column({ space: 5 }) {
            Text("Index0" + item)
              .fontSize(25)
              .fontColor(this.current === item ? '#0A59F7' : '#999')
              .fontFamily('source-sans-pro,cursive,sans-serif')
          }
          .onClick(() => {
            this.current = item
          })
        }, (item: string) => item)
      }
      .visibility(this.showFlag)
      .transition(TransitionEffect.OPACITY.animation({duration: 1000}).combine(TransitionEffect.translate({x: 100})))
      .width('100%')
      .justifyContent(FlexAlign.SpaceEvenly)
      .backgroundColor(Color.Pink)

      Column() {
        Button('打开侧边栏').onClick(() => {
          this.showFlag=Visibility.Visible
          this.showSideBar = true
        })

        Button('关闭侧边栏').onClick(() => {
          this.cancel()
        })
      }
      .backgroundColor(Color.Green)
      .gesture(
        TapGesture({ count: 2 })
          .onAction((event?: GestureEvent) => {
            if (event) {
              // this.showSideBar = false
              this.cancel()
            }
          })
      )
      // .margin({ top: 50, left: 20, right: 30 })
    }
    .sideBarPosition(SideBarPosition.End)
    .showSideBar(this.showSideBar)
    .showControlButton(false)
    .sideBarWidth(150)
    .minSideBarWidth(50)
    .maxSideBarWidth(300)
    .minContentWidth(0)
    .onChange((value: boolean) => {
      console.info('status:' + value)
    })
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进