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)  
    })  
  }  
}
  • 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.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
分享
微博
QQ
微信
回复
2024-09-26 15:35:16
相关问题
HarmonyOS 事件
648浏览 • 1回复 待解决
HarmonyOS 手势滑动登录UI实现
762浏览 • 1回复 待解决
HarmonyOS 如何禁止系统返回
483浏览 • 1回复 待解决
实现二次退出应用
2799浏览 • 1回复 待解决