在NavDestination子页面中实现该共享元素转场场景,步骤如下:
aboutToAppear(): void { this.navPathStack.replacePath({ name: 'nextA' }, false); } @Builder navPathMapBuilder(name: string) { if (name === 'nextA') { NextA(); } else if (name === 'nextB') { NextB(); } } build() { Navigation(this.navPathStack) .navDestination(this.navPathMapBuilder) .hideNavBar(true); }
.geometryTransition('sharedId')
.onClick(() => { this.getUIContext().animateTo({ duration: 1000 }, () => { this.navPathStack.pushPath({ name: 'nextB' }, false); }); });
.onBackPressed(() => { this.getUIContext().animateTo({ duration: 1000 }, () => { this.navPathStack.pop(false); }); return true; });
完整示例参考如下:
@Entry @Component struct RotateGeometryTransition { @Provide('navPS') navPathStack: NavPathStack = new NavPathStack(); aboutToAppear(): void { this.navPathStack.replacePath({ name: 'nextA' }, false); } @Builder navPathMapBuilder(name: string) { if (name === 'nextA') { NextA(); } else if (name === 'nextB') { NextB(); } } build() { Navigation(this.navPathStack) .navDestination(this.navPathMapBuilder) .hideNavBar(true); } } @Component struct NextA { @Consume('navPS') navPathStack: NavPathStack; build() { NavDestination() { Column() { Row() { Text('hello world').fontSize(16); } .width('100%') .height(200) .justifyContent(FlexAlign.Center) .backgroundColor(Color.Gray) .geometryTransition('sharedId') .onClick(() => { this.getUIContext().animateTo({ duration: 1000 }, () => { this.navPathStack.pushPath({ name: 'nextB' }, false); }); }); } .width('100%') .height('100%'); } .title('Next A'); } } @Component struct NextB { @Consume('navPS') navPathStack: NavPathStack; build() { NavDestination() { Column() { Row() { Row() { Text('hello world !!!').fontSize(32); }.rotate({ angle: 90 }) .width('100%') .height('100%'); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) .backgroundColor(Color.Gray) .geometryTransition('sharedId') .onClick(() => { this.getUIContext().animateTo({ duration: 1000 }, () => { this.navPathStack.pop(false); }); }); } .width('100%') .height('100%'); } .title('Next B') .onBackPressed(() => { this.getUIContext().animateTo({ duration: 1000 }, () => { this.navPathStack.pop(false); }); return true; }); } }