中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
HarmonyOS SideBarContainer如何实现手势滑动控制侧滑栏的开关和关联页面的移入移出效果?
微信扫码分享
@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) }) } }