HarmonyOS SideBarContainer如何实现手势滑动控制侧滑栏的开关和关联页面的移入移出效果?

HarmonyOS  SideBarContainer如何实现手势滑动控制侧滑栏的开关和关联页面的移入移出效果?

HarmonyOS
2024-09-26 13:11:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu
@Entry  
@Component  
struct SideBarContainerDemo {  
  @State arr: number[] = [1, 2, 3]  
  @State current: number = 1  
  @State showSideBar: boolean = false  
  @State showFlag: Visibility = Visibility.Hidden;  
  private panOptionLeft: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left })  
  private panOptionRigth: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right })  
  // 延迟关闭侧边栏,让自定义的出场动画显示  
  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() {  
  
      }  
      .backgroundColor(Color.Green)  
      // 左右拖动触发该手势事件  
      .gesture(  
        GestureGroup(GestureMode.Parallel,  
          PanGesture(this.panOptionLeft)  
            .onActionStart(() => {  
              console.info('pan start')  
            })  
            .onActionUpdate((event?: GestureEvent) => {  
              if (event) {  
                this.showFlag=Visibility.Visible  
                this.showSideBar = true  
              }  
              console.info('pan update')  
            })  
            .onActionEnd(() => {  
  
              console.info('pan end')  
            }),  
          PanGesture(this.panOptionRigth)  
            .onActionStart(() => {  
              console.info('pan start')  
            })  
            .onActionUpdate((event?: GestureEvent) => {  
              if (event) {  
  
              }  
              this.cancel()  
              console.info('pan update')  
            })  
            .onActionEnd(() => {  
              console.info('pan end')  
            })  
        )  
      )  
    }  
    .divider(null)  
    .sideBarPosition(SideBarPosition.End)  
    .showSideBar(this.showSideBar)  
    .showControlButton(false)  
    .sideBarWidth(150)  
    .minSideBarWidth(50)  
    .maxSideBarWidth(300)  
    .minContentWidth(0)  
    .onChange((value: boolean) => {  
      console.info('status:' + value)  
    })  
  }  
}
分享
微博
QQ
微信
回复
2024-09-26 15:35:16
相关问题
HarmonyOS 事件
29浏览 • 1回复 待解决
HarmonyOS 怎么禁止
151浏览 • 1回复 待解决
实现二次退出应用
1837浏览 • 1回复 待解决