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
相关问题
实现二次退出应用
1722浏览 • 1回复 待解决
滑动组件如何实现单边spring效果
824浏览 • 1回复 待解决
HarmonyOS 如何从子组件控制手势分发
368浏览 • 1回复 待解决
实现层叠广告滑动效果
768浏览 • 1回复 待解决
如何控制软键盘弹出对页面的遮挡?
2670浏览 • 1回复 待解决
HarmonyOS如何实现hap包页面的跳转
495浏览 • 1回复 待解决