中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
想实现将弹窗框改为占满屏幕宽和到最下方屏幕底部边缘,以及弹窗效果改为从屏幕下方边缘向上拉起,退出则反向拉回至底部隐藏。
微信扫码分享
const INIT_OFFSET: number = 400; @Component export struct CustomDialogC { @State heightSize: string = "100%"; @State offsetY: number = INIT_OFFSET; @State positionY: number = INIT_OFFSET; build() { NavDestination() { Stack({ alignContent: Alignment.Bottom }) { Column() { } .width("100%") .height(this.heightSize) .onClick(() => { console.log(this.heightSize) animateTo({ duration: 300, curve: Curve.Friction, onFinish: () => { AppRouter.pop(); } }, () => { this.heightSize = "0%" }) }) Column() { Text("弹窗") .fontColor(Color.White) } .width("100%") .backgroundColor(Color.Blue) .height(this.heightSize) .offset({ x: 0, y: this.offsetY }) .gesture( PanGesture({ direction: PanDirection.Vertical, distance: 1 }) .onActionUpdate((event?: GestureEvent) => { this.handlePanGestureUpdate(event?.offsetY); }) .onActionEnd((event?: GestureEvent) => { this.handlePanGestureEnd(event?.offsetY); }) ) .transition( TransitionEffect.move(TransitionEdge.BOTTOM) .animation({ duration: 500, curve: Curve.Friction }) ) } .width("100%") .height("100%") } .mode(NavDestinationMode.DIALOG) .hideTitleBar(true) } handlePanGestureUpdate(offsetY?: number): void { if (!offsetY || (offsetY < 0 && this.positionY < INIT_OFFSET)) { return; } console.log(`Update offsetY:${offsetY}`) this.offsetY = this.positionY + offsetY!; } handlePanGestureEnd(offsetY?: number): void { if (!offsetY) { return; } console.log(`End offsetY:${offsetY}`) if (this.positionY < INIT_OFFSET) { if (offsetY > 300) { this.close(); } else if (offsetY > 150) { this.reset(); } else { animateTo({ curve: Curve.Friction, }, () => { this.positionY = this.offsetY; }) } } else { if (offsetY < -200) { animateTo({ curve: Curve.Friction, }, () => { this.offsetY = 0; this.positionY = this.offsetY; }) } else if (offsetY > 200) { this.close(); } else { this.reset(); } } } private reset() { animateTo({ curve: Curve.Friction }, () => { this.offsetY = INIT_OFFSET; this.positionY = this.offsetY; }); } private close() { animateTo({ curve: Curve.Friction, onFinish: () => { AppRouter.pop(); } }, () => { this.heightSize = "0%"; }); } }