您参考下pageTransition页面转场看能否实现您要效果,
参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-page-transition-animation-V5#示例1
您可参考以下demo:
// demo.ets
interface AnimateCallback {
finish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
start: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
onFinish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined
timeout: (number | undefined) | undefined;
}
const customTransitionMap: Map<number, AnimateCallback> = new Map()
export class CustomTransition {
private constructor() {
}
static delegate = new CustomTransition();
static getInstance() {
return CustomTransition.delegate;
}
registerNavParam(name: number, startCallback: (operation: boolean, isExit: boolean) => void,
endCallback: (operation: boolean, isExit: boolean) => void,
onFinish: (opeation: boolean, isExit: boolean) => void, timeout: number): void {
console.log("hxl registerNavParam");
if (customTransitionMap.has(name)) {
let param = customTransitionMap.get(name);
if (param != undefined) {
param.start = startCallback;
param.finish = endCallback;
param.onFinish = onFinish;
param.timeout = timeout;
return;
}
}
let params: AnimateCallback = {
timeout: timeout,
start: startCallback,
finish: endCallback,
onFinish: onFinish
};
customTransitionMap.set(name, params);
}
unRegisterNavParam(name: number): void {
console.log("hxl unRegisterNavParam");
customTransitionMap.delete(name);
}
getAnimateParam(name: number): AnimateCallback {
console.log("hxl unRegisterNavParam");
let result: AnimateCallback = {
start: customTransitionMap.get(name)?.start,
finish: customTransitionMap.get(name)?.finish,
timeout: customTransitionMap.get(name)?.timeout,
onFinish: customTransitionMap.get(name)?.onFinish
};
return result;
}
}
@Entry
@Component
struct Index {
@Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
@Builder
PageMap(name: string) {
if (name === 'pageOne') {
pageOneTmp({ pageId: Date.now() })
} else if (name === 'pageTwo') {
PageTwoTemp({ pageId: Date.now() })
}
}
aboutToAppear() {
if (this.pageInfos === undefined) {
this.pageInfos = new NavPathStack();
}
this.pageInfos.pushPath({ name: 'pageOne' }, false)
}
build() {
Navigation(this.pageInfos) {
}.title('NavIndex').navDestination(this.PageMap)
.hideNavBar(true)
.customNavContentTransition((from: NavContentInfo, to: NavContentInfo, operation: NavigationOperation) => {
if (from.mode == NavDestinationMode.DIALOG || to.mode == NavDestinationMode.DIALOG) {
return undefined;
}
console.log(`hxl info: ${to.name}, index: ${to.index}, mode: ${to.mode}`);
console.log(`hxl pre info: ${from.name}, index: ${from.index}, mode: ${from.mode}`);
console.log(`hxl operation: ${operation}`)
if (from.index === -1 || to.index === -1) {
console.log("hxl undefined")
return undefined;
}
let customAnimation: NavigationAnimatedTransition = {
onTransitionEnd: (isSuccess: boolean) => {
console.log(`hxl current transition result is ${isSuccess}`);
},
timeout: 700,
transition: (transitionProxy: NavigationTransitionProxy) => {
console.log("hxl trigger transition callback");
let fromParam: AnimateCallback = CustomTransition.getInstance()?.getAnimateParam(from.index);
let toParam: AnimateCallback = CustomTransition.getInstance()?.getAnimateParam(to.index);
if (fromParam.start != undefined) {
console.log("hxl fromParam.start");
// push, x = 0
fromParam.start(operation == NavigationOperation.PUSH, true);
}
if (toParam.start != undefined) {
console.log("hxl toParam.start");
// push, x = 300
toParam.start(operation == NavigationOperation.PUSH, false);
}
animateTo({
duration: 1200, onFinish: () => {
console.log("hxl animateTo start");
if (fromParam.onFinish != undefined) {
console.log("hxl fromParam onFinish");
// push 0
fromParam.onFinish(operation === NavigationOperation.PUSH, true);
}
if (toParam.onFinish != undefined) {
console.log("hxl toParam onFinish");
// push 0
toParam.onFinish(operation === NavigationOperation.PUSH, false);
}
transitionProxy.finishTransition();
}
}, () => {
if (fromParam.finish != undefined) {
console.log("hxl fromParam finish");
// push x = -300
fromParam?.finish(operation === NavigationOperation.PUSH, true)
}
if (toParam.finish != undefined) {
console.log("hxl toParam finish");
// push x = 0
toParam?.finish(operation === NavigationOperation.PUSH, false);
}
})
}
};
return customAnimation;
})
}
}
@Component
struct pageOneTmp {
@Consume('pageInfos') pageInfos: NavPathStack
@State x: number = 0
@State scaleVal: number = 1
pageId: number = 0;
aboutToAppear() {
this.pageId = this.pageInfos.getAllPathName().length - 1;
CustomTransition.getInstance().registerNavParam(this.pageId, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? 0 : 300;
}, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? -300 : 0;
}, (isPush: boolean, isExit: boolean) => {
console.log("hxl PageTwo x finish : " + this.x)
this.x = 0;
}, 200);
}
build() {
NavDestination() {
Column() {
Button('pageTwo', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageInfos.pushPathByName('pageTwo', null) //将name指定的NavDestination页面信息入栈,传递的数据为param
})
}.width('100%').height('100%')
}
.title('pageOne')
.mode(NavDestinationMode.STANDARD)
.onBackPressed(() => {
const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))
return true
})
.onDisAppear(() => {
console.log("hxl pageone onDisAppear");
CustomTransition.getInstance().unRegisterNavParam(this.pageId)
})
.translate({ x: 0, y: this.x, z: 0 })
.backgroundColor(Color.White)
}
}
@Component
struct PageTwoTemp {
@Consume('pageInfos') pageInfos: NavPathStack
@State x: number = 300
pageId: number = 0;
aboutToAppear() {
this.pageId = this.pageInfos.getAllPathName().length - 1;
CustomTransition.getInstance().registerNavParam(this.pageId, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? 0 : isPush ? 300 : -300;
console.log("hxl PageTwo x start is : " + this.x)
}, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? isPush ? -300 : 300 : 0;
console.log("hxl PageTwo x end is : " + this.x)
}, (isPush: boolean, isExit: boolean) => {
this.x = 0;
console.log("hxl PageTwo x finish : " + this.x)
}, 2000);
}
build() {
NavDestination() {
Column() {
Text('Page Two').fontSize(50)
}.width('100%').height('100%')
}
.title('pageTwo')
.onBackPressed(() => {
console.log("hxl pagetwo onBackPressed");
const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))
return true
})
.onDisAppear(() => {
console.log("hxl pagetwo onDisAppear");
CustomTransition.getInstance().unRegisterNavParam(this.pageId)
})
.translate({ x: 0, y: 0, z: this.x })
.backgroundColor(Color.White)
}
}
您看下这种是否是您需要的效果:
interface AnimateCallback {
finish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
start: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
onFinish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
timeout: (number | undefined) | undefined;
}
const customTransitionMap: Map<number, AnimateCallback> = new Map();
export class CustomTransition {
private constructor() {
}
static delegate = new CustomTransition();
static getInstance() {
return CustomTransition.delegate;
}
registerNavParam(name: number, startCallback: (operation: boolean, isExit: boolean) => void,
endCallback: (operation: boolean, isExit: boolean) => void,
onFinish: (operation: boolean, isExit: boolean) => void, timeout: number): void {
console.log("hxl registerNavParam");
if (customTransitionMap.has(name)) {
let param = customTransitionMap.get(name);
if (param != undefined) {
param.start = startCallback;
param.finish = endCallback;
param.onFinish = onFinish;
param.timeout = timeout;
return;
}
}
let params: AnimateCallback = {
timeout: timeout,
start: (isPush, isExit) => {
startCallback(isPush, isExit);
this.setOpacity(isExit ? 0 : 1); // 设置透明度
},
finish: (isPush, isExit) => {
endCallback(isPush, isExit);
this.setOpacity(isExit ? 0 : 1); // 设置透明度
},
onFinish: (isPush, isExit) => {
onFinish(isPush, isExit);
this.setOpacity(1); // 重置透明度
}
};
customTransitionMap.set(name, params);
}
unRegisterNavParam(name: number): void {
console.log("hxl unRegisterNavParam");
customTransitionMap.delete(name);
}
getAnimateParam(name: number): AnimateCallback {
console.log("hxl unRegisterNavParam");
let result: AnimateCallback = {
start: customTransitionMap.get(name)?.start,
finish: customTransitionMap.get(name)?.finish,
timeout: customTransitionMap.get(name)?.timeout,
onFinish: customTransitionMap.get(name)?.onFinish
};
return result;
}
setOpacity(opacity: number) {
// 实现设置透明度的方法
}
}
@Entry
@Component
struct Index {
@Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
@Builder
PageMap(name: string) {
if (name === 'pageOne') {
pageOneTmp({ pageId: Date.now() })
} else if (name === 'pageTwo') {
PageTwoTemp({ pageId: Date.now() })
}
}
aboutToAppear() {
if (this.pageInfos === undefined) {
this.pageInfos = new NavPathStack();
}
this.pageInfos.pushPath({ name: 'pageOne' }, false)
}
build() {
Navigation(this.pageInfos) {
}.title('NavIndex').navDestination(this.PageMap)
.hideNavBar(true)
.customNavContentTransition((from: NavContentInfo, to: NavContentInfo, operation: NavigationOperation) => {
if (from.mode == NavDestinationMode.DIALOG || to.mode == NavDestinationMode.DIALOG) {
return undefined;
}
console.log(`hxl info: ${to.name}, index: ${to.index}, mode: ${to.mode}`);
console.log(`hxl pre info: ${from.name}, index: ${from.index}, mode: ${from.mode}`);
console.log(`hxl operation: ${operation}`)
if (from.index === -1 || to.index === -1) {
console.log("hxl undefined")
return undefined;
}
let customAnimation: NavigationAnimatedTransition = {
onTransitionEnd: (isSuccess: boolean) => {
console.log(`hxl current transition result is ${isSuccess}`);
},
timeout: 700,
transition: (transitionProxy: NavigationTransitionProxy) => {
console.log("hxl trigger transition callback");
let fromParam: AnimateCallback = CustomTransition.getInstance()?.getAnimateParam(from.index);
let toParam: AnimateCallback = CustomTransition.getInstance()?.getAnimateParam(to.index);
if (fromParam.start != undefined) {
console.log("hxl fromParam.start");
fromParam.start(operation == NavigationOperation.PUSH, true);
}
if (toParam.start != undefined) {
console.log("hxl toParam.start");
toParam.start(operation == NavigationOperation.PUSH, false);
}
animateTo({
duration: 1200, onFinish: () => {
console.log("hxl animateTo start");
if (fromParam.onFinish != undefined) {
console.log("hxl fromParam onFinish");
fromParam.onFinish(operation === NavigationOperation.PUSH, true);
}
if (toParam.onFinish != undefined) {
console.log("hxl toParam onFinish");
toParam.onFinish(operation === NavigationOperation.PUSH, false);
}
transitionProxy.finishTransition();
}
}, () => {
if (fromParam.finish != undefined) {
console.log("hxl fromParam finish");
fromParam?.finish(operation === NavigationOperation.PUSH, true);
}
if (toParam.finish != undefined) {
console.log("hxl toParam finish");
toParam?.finish(operation === NavigationOperation.PUSH, false);
}
})
}
}
return customAnimation;
})
}
}
@Component
struct pageOneTmp {
@Consume('pageInfos') pageInfos: NavPathStack
@State x: number = 0
@State opacitys: number = 1 // 添加透明度状态
pageId: number = 0;
aboutToAppear() {
this.pageId = this.pageInfos.getAllPathName().length - 1;
CustomTransition.getInstance().registerNavParam(this.pageId, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? 0 : 300;
this.opacitys = isExit ? 1 : 0; // 设置初始透明度
}, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? -300 : 0;
this.opacitys = isExit ? 0 : 1; // 设置结束透明度
}, (isPush: boolean, isExit: boolean) => {
console.log("hxl PageOne x finish : " + this.x)
this.x = 0;
this.opacitys = 1; // 重置透明度
}, 200);
}
build() {
NavDestination() {
Column() {
Button('pageTwo', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageInfos.pushPathByName('pageTwo', null) //将name指定的NavDestination页面信息入栈,传递的数据为param
})
}.width('100%').height('100%').backgroundColor(Color.Red)
}
.title('pageOne')
.mode(NavDestinationMode.STANDARD)
.onBackPressed(() => {
const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))
return true
})
.onDisAppear(() => {
console.log("hxl pageone onDisAppear");
CustomTransition.getInstance().unRegisterNavParam(this.pageId)
})
.translate({ x: 0, y: this.x, z: 0 })
.opacity(this.opacitys) // 设置透明度属性
.backgroundColor(Color.White)
}
}
@Component
struct PageTwoTemp {
@Consume('pageInfos') pageInfos: NavPathStack
@State x: number = 300
@State opacitys: number = 1 // 添加透明度状态
pageId: number = 0;
aboutToAppear() {
this.pageId = this.pageInfos.getAllPathName().length - 1;
CustomTransition.getInstance().registerNavParam(this.pageId, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? 0 : isPush ? 300 : -300;
this.opacitys = isExit ? 1 : 0; // 设置初始透明度
console.log("hxl PageTwo x start is : " + this.x)
}, (isPush: boolean, isExit: boolean) => {
this.x = isExit ? isPush ? -300 : 300 : 0;
this.opacitys = isExit ? 0 : 1; // 设置结束透明度
console.log("hxl PageTwo x end is : " + this.x)
}, (isPush: boolean, isExit: boolean) => {
this.x = 0;
this.opacitys = 1; // 重置透明度
console.log("hxl PageTwo x finish : " + this.x)
}, 2000);
}
build() {
NavDestination() {
Column() {
Text('Page Two').fontSize(50)
}.width('100%').height('100%').backgroundColor(Color.Blue)
}
.title('pageTwo')
.onBackPressed(() => {
console.log("hxl pagetwo onBackPressed");
const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))
return true
})
.onDisAppear(() => {
console.log("hxl pagetwo onDisAppear");
CustomTransition.getInstance().unRegisterNavParam(this.pageId)
})
.translate({ x: 0, y: 0, z: this.x })
.opacity(this.opacitys) // 设置透明度属性
.backgroundColor(Color.White)
}
}