华为仓颉鸿蒙HarmonyOS NEXT原生UI转场动画之页面间转场

117 阅读18分钟

本文篇幅较长,建议点赞收藏,以免找不到哟(^U^)ノ~YO

当路由进行切换时,可以通过在 onTransition 函数中自定义页面入场和页面退场的转场动效。

函数

onTransition()

protected open func onTransition(): Unit

进入此页面或移动到其他页面时实现动画。

自定义类型

PageTransitionEnter

public class PageTransitionEnter {        public init(value: PageTransitionOptions)        public func slide(value: SlideEffect): This        public func translate(x!: Length = 0.vp, y!: Length = 0.vp, z!: Length = 0.vp): This        public func translate(x!: Length = 0.vp, y!: Length = 0.vp, z!: Length = 0.vp): This        public func translate(x!: Int32 = 0, y!: Int32 = 0, z!: Int32 = 0): This        public func translate(x!: CJResource, y!: CJResource, z!: CJResource): This        public func translate(x!: Int64 = 0, y!: Int64 = 0, z!: Int64 = 0): This        public func translate(x!: Float64 = 0.0, y!: Float64 = 0.0, z!: Float64 = 0.0): This        public func translate(value: Length): This        public func translate(value: CJResource): This        public func translate(value: Int64): This        public func translate(value: Float64): This        public func translateX(value: Length): This        public func translateX(value: CJResource): This        public func translateX(value: Int64): This        public func translateX(value: Float64): This        public func translateY(value: Length): This        public func translateY(value: CJResource): This        public func translateY(value: Int64): This        public func translateY(value: Float64): This        public func scale(scaleValue: Float32): This        public func scale(scaleValue: Int32): This        public func scale(x!: Float32 = 1.0, y!: Float32 = 1.0, z!: Float32 = 1.0, centerX!: Length = 50.percent, centerY!: Length = 50.percent): This        public func scale(x!: Float32 = 1.0, y!: Float32 = 1.0, z!: Float32 = 1.0, centerX!: CJResource, centerY!: CJResource): This        public func scaleX(scale: Float32): This        public func scaleX(scale: Int32): This        public func scaleY(scale: Float32): This        public func scaleY(scale: Int32): This        public func opacity(value: Float64): This        public func opacity(value: Int64): This    }

页面的自定义入场动效类型。

init(PageTransitionOptions)

public init(value: PageTransitionOptions)

创建一个 PageTransitionExit 类型的对象。

参数名参数类型必填默认值描述
valuePageTransitionOptions-配置入场动效的参数。

slide(SlideEffect)

public func slide(value: SlideEffect): This

设置页面转场时的滑入滑出效果。

参数名参数类型必填默认值描述
valueSlideEffect-页面转场时的滑入滑出效果。

translate(Length, Length, Length)

public func translate(x!: Length = 0.vp, y!: Length = 0.vp, z!: Length = 0.vp): This

设置页面转场时的平移效果,为入场时起点和退场时终点的值,和slide同时设置时默认生效slide。

参数名参数类型必填默认值描述
xLength0.vp横向的平移距离。
yLength0.vp纵向的平移距离。
zLength0.vp竖向的平移距离。

translate(Float64, Float64, Float64)

public func translate(x!: Float64 = 0.0, y!: Float64 = 0.0, z!: Float64 = 0.0)
参数名参数类型必填默认值描述
xFloat640.0横向的平移距离。
yFloat640.0纵向的平移距离。
zFloat640.0竖向的平移距离。

translate(Int32, Int32, Int32)

public func translate(x!: Int32 = 0, y!: Int32 = 0, z!: Int32 = 0): This
参数名参数类型必填默认值描述
xInt320横向的平移距离。
yInt320纵向的平移距离。
zInt320竖向的平移距离。

translate(CJResource, CJResource, CJResource)

public func translate(x!: CJResource, y!: CJResource, z!: CJResource): This

设置页面转场时的平移效果,为入场时起点和退场时终点的值,和slide同时设置时默认生效slide。

参数名参数类型必填默认值描述
xCJResource-横向的平移距离。
yCJResource-纵向的平移距离。
zCJResource-竖向的平移距离。

translate(Int64, Int64, Int64)

public func translate(x!Int64 = 0, y!Int64 = 0, z!Int64 = 0): This

设置页面转场时的平移效果,为入场时起点和退场时终点的值,和slide同时设置时默认生效slide。

参数名参数类型必填默认值描述
xInt640横向的平移距离。
yInt640纵向的平移距离。
zInt640竖向的平移距离。

translate(Float64, Float64, Float64)

public func translate(x!Float64 = 0.0, y!Float64 = 0.0, z!Float64 = 0.0): This

设置页面转场时的平移效果,为入场时起点和退场时终点的值,和slide同时设置时默认生效slide。

参数名参数类型必填默认值描述
xFloat640.0横向的平移距离。
yFloat640.0纵向的平移距离。
zFloat640.0竖向的平移距离。

translate(Length)

public func translate(value: Length): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
valueLength-平移距离。

translate(CJResource)

public func translate(value: CJResource): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
valueCJResource-平移距离。

translate(Int64)

public func translate(value: Int64): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
valueInt64-平移距离。

translate(Float64)

设置页面转场时的平移效果。

public func translate(value: Float64): This
参数名参数类型必填默认值描述
valueFloat64-平移距离。

translateX(Length)

public func translateX(value: Length): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueLength-横向平移距离。

translateX(CJResource)

public func translateX(value: CJResource): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueCJResource-横向平移距离。

translateX(Int64)

public func translateX(value: Int64): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueInt64-横向平移距离。

translateX(Float64)

public func translateX(value: Float64): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueFloat64-横向平移距离。

translateY(Length)

public func translateY(value: Length): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueLength-纵向平移距离。

translateY(CJResource)

public func translateY(value: CJResource): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueCJResource-纵向平移距离。

translateY(Int64)

public func translateY(value: Int64): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueInt64-纵向平移距离。

translateY(Float64)

public func translateY(value: Float64): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueFloat64-纵向平移距离。

scale(Float32)

public func scale(scaleValue: Float32): This

设置页面转场时的缩放效果。

参数名参数类型必填默认值描述
scaleValueFloat32-放大倍数(或缩小比例)。

scale(Int32)

public func scale(scaleValue: Int32): This

设置页面转场时的缩放效果。

参数名参数类型必填默认值描述
scaleValueInt32-放大倍数(或缩小比例)。

scale(Float32, Float32, Float32, Length, Length)

public func scale(            x!: Float32 = 1.0,            y!: Float32 = 1.0,            z!: Float32 = 1.0,            centerX!: Length = 50.percent,            centerY!: Length = 50.percent        ): This 

设置页面转场时的缩放效果,为入场时起点和退场时终点的值。

参数名参数类型必填默认值描述
xFloat321.0横向放大倍数(或缩小比例)。
yFloat321.0纵向放大倍数(或缩小比例)。
zFloat321.0竖向放大倍数(或缩小比例)。
centerXLength50.percent中心点坐标。
centerYLength50.percent中心点坐标。

scale(Float32, Float32, Float32, CJResource, CJResource)

public func scale(        x!: Float32,        y!: Float32,        z!: Float32,        centerX!: CJResource,        centerY!: CJResource    ): This

设置页面转场时的缩放效果,为入场时起点和退场时终点的值。

参数名参数类型必填默认值描述
xFloat32-横向放大倍数(或缩小比例)。
yFloat32-纵向放大倍数(或缩小比例)。
zFloat32-竖向放大倍数(或缩小比例)。
centerXCJResource-中心点坐标。
centerYCJResource-中心点坐标。

scaleX(Float32)

public func scaleX(scale: Float32): This

设置页面转场时的横向缩放效果。

参数名参数类型必填默认值描述
scaleFloat32-放大倍数(或缩小比例)。

scaleX(Int32)

public func scaleX(scale: Int32): This

设置页面转场时的横向缩放效果。

参数名参数类型必填默认值描述
scaleInt32-放大倍数(或缩小比例)。

scaleY(Float32)

public func scaleY(scale: Float32): This

设置页面转场时的纵向缩放效果。

参数名参数类型必填默认值描述
scaleFloat32-放大倍数(或缩小比例)。

scaleY(Int32)

public func scaleY(scale: Int32): This

设置页面转场时的纵向缩放效果。

参数名参数类型必填默认值描述
scaleInt32-放大倍数(或缩小比例)。

opacity(Float64)

public func opacity(value: Float64): This

设置入场的起点透明度值或者退场的终点透明度值。

参数名参数类型必填默认值描述
valueFloat64-入场的起点透明度值或者退场的终点透明度值。

opacity(Int64)

public func opacity(value: Int64): This

设置入场的起点透明度值或者退场的终点透明度值。

参数名参数类型必填默认值描述
valueInt64-入场的起点透明度值或者退场的终点透明度值。

PageTransitionExit

public class PageTransitionExit {        public init(value: PageTransitionOptions)        public func slide(value: SlideEffect): This        public func translate(x!: Length = 0.vp, y!: Length = 0.vp, z!: Length = 0.vp): This        public func translate(x!: Length = 0.vp, y!: Length = 0.vp, z!: Length = 0.vp): This        public func translate(x!: Int32 = 0, y!: Int32 = 0, z!: Int32 = 0): This        public func translate(x!: CJResource, y!: CJResource, z!: CJResource): This        public func translate(x!: Int64 = 0, y!: Int64 = 0, z!: Int64 = 0): This        public func translate(x!: Float64 = 0.0, y!: Float64 = 0.0, z!: Float64 = 0.0): This        public func translate(value: Length): This        public func translate(value: CJResource): This        public func translate(value: Int64): This        public func translate(value: Float64): This        public func translateX(value: Length): This        public func translateX(value: CJResource): This        public func translateX(value: Int64): This        public func translateX(value: Float64): This        public func translateY(value: Length): This        public func translateY(value: CJResource): This        public func translateY(value: Int64): This        public func translateY(value: Float64): This        public func scale(scaleValue: Float32): This        public func scale(scaleValue: Int32): This        public func scale(scaleValue: Float32): This        public func scale(scaleValue: Int32): This        public func scale(x!: Float32 = 1.0, y!: Float32 = 1.0, z!: Float32 = 1.0, centerX!: Length = 50.percent, centerY!: Length = 50.percent): This        public func scale(x!: Float32 = 1.0, y!: Float32 = 1.0, z!: Float32 = 1.0, centerX!: CJResource, centerY!: CJResource): This        public func scaleX(scale: Float32): This        public func scaleX(scale: Int32): This        public func scaleY(scale: Float32): This        public func scaleY(scale: Int32): This        public func opacity(value: Float64): This        public func opacity(value: Int64): This    }

页面的自定义退场动效类型。

init(PageTransitionOptions)

public init(value: PageTransitionOptions)

创建一个 PageTransitionExit 类型的对象。

参数名参数类型必填默认值描述
valuePageTransitionOptions-配置退场动效的参数。

slide(SlideEffect)

public func slide(value: SlideEffect): This

设置页面转场时的滑入滑出效果。

参数名参数类型必填默认值描述
valueSlideEffect-页面转场时的滑入滑出效果。

translate(Length, Length, Length)

public func translate(x!: Length = 0.vp, y!: Length = 0.vp, z!: Length = 0.vp): This

设置页面转场时的平移效果,为入场时起点和退场时终点的值,和slide同时设置时默认生效slide。

参数名参数类型必填默认值描述
xLength0.vp横向的平移距离。
yLength0.vp纵向的平移距离。
zLength0.vp竖向的平移距离。

translate(Float64, Float64, Float64)

public func translate(x!: Float64 = 0.0, y!: Float64 = 0.0, z!: Float64 = 0.0)
参数名参数类型必填默认值描述
xFloat640.0横向的平移距离。
yFloat640.0纵向的平移距离。
zFloat640.0竖向的平移距离。

translate(Int32, Int32, Int32)

public func translate(x!: Int32 = 0, y!: Int32 = 0, z!: Int32 = 0): This
参数名参数类型必填默认值描述
xInt320横向的平移距离。
yInt320纵向的平移距离。
zInt320竖向的平移距离。

translate(CJResource, CJResource, CJResource)

public func translate(x!: CJResource, y!: CJResource, z!: CJResource): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
xCJResource-横向的平移距离。
yCJResource-纵向的平移距离。
zCJResource-竖向的平移距离。

translate(Int64, Int64, Int64)

public func translate(x!Int64 = 0, y!Int64 = 0, z!Int64 = 0): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
xInt640横向的平移距离。
yInt640纵向的平移距离。
zInt640竖向的平移距离。

translate(Float64, Float64, Float64)

public func translate(x!: Float64 = 0.0, y!: Float64 = 0.0, z!: Float64 = 0.0): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
xFloat640.0横向的平移距离。
yFloat640.0纵向的平移距离。
zFloat640.0竖向的平移距离。

translate(Length)

public func translate(value: Length): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
valueLength-平移距离。

translate(CJResource)

public func translate(value: CJResource): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
valueCJResource-平移距离。

translate(Int64)

public func translate(value: Int64): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
valueInt64-平移距离。

translate(Float64)

public func translate(value: Float64): This

设置页面转场时的平移效果。

参数名参数类型必填默认值描述
valueFloat64-平移距离。

translateX(Length)

public func translateX(value: Length): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueLength-横向平移距离。

translateX(CJResource)

public func translateX(value: CJResource): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueCJResource-横向平移距离。

translateX(Int64)

public func translateX(value: Int64): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueInt64-横向平移距离。

translateX(Float64)

public func translateX(value: Float64): This

设置页面转场时的横向平移效果。

参数名参数类型必填默认值描述
valueFloat64-横向平移距离。

translateY(Length)

public func translateY(value: Length): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueLength-纵向平移距离。

translateY(CJResource)

public func translateY(value: CJResource): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueCJResource-纵向平移距离。

translateY(Int64)

public func translateY(value: Int64): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueInt64-纵向平移距离。

translateY(Float64)

public func translateY(value: Float64): This

设置页面转场时的纵向平移效果。

参数名参数类型必填默认值描述
valueFloat64-纵向平移距离。

scale(scaleValue: Float32)

public func scale(scaleValue: Float32): This

设置页面转场时的缩放效果。

参数名参数类型必填默认值描述
scaleValueFloat32-放大倍数(或缩小比例)。

scale(scaleValue: Int32)

public func scale(scaleValue: Int32): This

设置页面转场时的缩放效果。

参数名参数类型必填默认值描述
scaleValueInt32-放大倍数(或缩小比例)。

scale(Float32, Float32, Float32, Length, Length)

public func scale(            x!: Float32 = 1.0,            y!: Float32 = 1.0,            z!: Float32 = 1.0,            centerX!: Length = 50.percent,            centerY!: Length = 50.percent        ): This 

设置页面转场时的缩放效果,为入场时起点和退场时终点的值。

参数名参数类型必填默认值描述
xFloat321.0横向放大倍数(或缩小比例)。
yFloat321.0纵向放大倍数(或缩小比例)。
zFloat321.0竖向放大倍数(或缩小比例)。
centerXLength50.percent中心点坐标。
centerYLength50.percent中心点坐标。

scale(Float32, Float32, Float32, CJResource, CJResource)

public func scale(            x!: Float32,            y!: Float32,            z!: Float32,            centerX!: CJResource,            centerY!: CJResource        ): This 

设置页面转场时的缩放效果,为入场时起点和退场时终点的值。

参数名参数类型必填默认值描述
xFloat32-横向放大倍数(或缩小比例)。
yFloat32-纵向放大倍数(或缩小比例)。
zFloat32-竖向放大倍数(或缩小比例)。
centerXCJResource-中心点坐标。
centerYCJResource-中心点坐标。

scaleX(Float32)

public func scaleX(scale: Float32): This

设置页面转场时的横向缩放效果。

参数名参数类型必填默认值描述
scaleFloat32-放大倍数(或缩小比例)。

scaleX(Int32)

public func scaleX(scale: Int32): This

设置页面转场时的横向缩放效果。

参数名参数类型必填默认值描述
scaleInt32-放大倍数(或缩小比例)。

scaleY(Float32)

public func scaleY(scale: Float32): This

设置页面转场时的纵向缩放效果。

参数名参数类型必填默认值描述
scaleFloat32-放大倍数(或缩小比例)。

scaleY(Int32)

public func scaleY(scale: Int32): This

设置页面转场时的纵向缩放效果。

参数名参数类型必填默认值描述
scaleInt32-放大倍数(或缩小比例)。

opacity(Float64)

public func opacity(value: Float64): This

设置入场的起点透明度值或者退场的终点透明度值。

参数名参数类型必填默认值描述
valueFloat64-入场的起点透明度值或者退场的终点透明度值。

opacity(Int64)

public func opacity(value: Int64): This

设置入场的起点透明度值或者退场的终点透明度值。

参数名参数类型必填默认值描述
valueInt64-入场的起点透明度值或者退场的终点透明度值。

PageTransitionOptions

init(RouteType, Int32, Curve, Int32)

public init(        `type`!: RouteType = RouteType.None,        duration!: Int32 = 1000,        curve!: Curve = Curve.Linear,        delay!: Int32 = 0    )

新建一个 PageTransitionOptions 类型对象。

说明

没有匹配时使用系统默认的页面转场效果(根据设备可能会有差异),如需禁用系统默认页面转场效果,可以指定duration为0。

参数名参数类型必填默认值描述
typeRouteTypeRouteType.None页面转场效果生效的路由类型。
durationInt321000动画的时长。
curveCurveCurve.Linear动画曲线。
delayInt320动画延迟时长。

事件

onEnter((RouteType, Float64)->Unit)

public func onEnter(event: (RouteType, Float64)->Unit): This
参数名参数类型描述
event(RouteType, Float64)->Unit回调入参为当前入场动画的归一化进度[0 - 1]。- type:RouteType 跳转方法。- progress:Float64 当前进度。触发该事件的条件:逐帧回调,直到入场动画结束,progress从0变化到1。

onExit((RouteType, Float64)->Unit)

public func onExit(event: (RouteType, Float64)->Unit): This
参数名参数类型描述
event(RouteType, Float64)->Unit回调入参为当前退场动画的归一化进度[0 - 1]。- type:RouteType 跳转方法。- progress:Float64 当前进度。触发该事件的条件:逐帧回调,直到退场动画结束,progress从0变化到1。

枚举说明

RouteType

枚举值描述
Push重定向指定页面。从PageB回退到之前的页面PageA。对于PageB,指定RouteType为None或者Pop的PageTransitionExit组件样式生效,对于PageA,指定RouteType为None或者Pop的PageTransitionEnter组件样式生效。
Pop跳转到下一页面。PageA跳转到下一个新的界面PageB。对于PageA,指定RouteType为None或者Push的PageTransitionExit组件样式生效,对于PageB,指定RouteType为None或者Push的PageTransitionEnter组件样式生效。
None页面未重定向。如Push和Pop描述中RouteType为None的情形,即页面进场时PageTransitionEnter的转场效果生效;退场时PageTransitionExit的转场效果生效。

SlideEffect

枚举值描述
Left设置到入场时表示从左边滑入,出场时表示滑出到左边。
Right设置到入场时表示从右边滑入,出场时表示滑出到右边。
Top设置到入场时表示从上边滑入,出场时表示滑出到上边。
Bottom设置到入场时表示从下边滑入,出场时表示滑出到下边。

示例代码

import ohos.base.*
import ohos.component.Column
import ohos.component.Row
import ohos.component.Button
import ohos.component.Text
import ohos.component.CustomView
import ohos.component.CJEntry
import ohos.component.loadNativeView
import ohos.component.*
import ohos.router.* 
import ohos.state_manage.SubscriberManager
import ohos.state_manage.ObservedProperty
import ohos.state_macro_manage.Entry
import ohos.state_macro_manage.Component
import ohos.state_macro_manage.State
import ohos.state_macro_manage.r

@Entry    
@Component 
class MyView {        
@State var scale2: Float32 = 1.0        
@State var opacity2: Float64 = 1.0            
func build() {            
            Column() {               Image(@r(app.media.background)).width(100.percent).height(100.percent)            }            .width(100.percent)            .height(100.percent)            .scale(x: scale2, y: 1.0)            .opacity(this.opacity2)            .onClick { e =>                Router.push(url: "Page1")            }        }            protected func onTransition(): Unit {            PageTransitionEnter(PageTransitionOptions(duration: 1200, curve: Curve.Linear))            .onEnter { ty: RouteType, progress: Float64 =>                match (ty) {                    case RouteType.Push | RouteType.Pop =>                        scale2 = Float32(progress)                        opacity2 = progress                    case _ => ()                }            }            PageTransitionExit(PageTransitionOptions(duration: 1200, curve: Curve.Ease))            .onExit { ty: RouteType, progress: Float64 =>                match (ty) {                    case RouteType.Push =>                        this.scale2 = Float32(1.0 - progress)                        this.opacity2 = 1.0 - progress                    case _ => ()                }            }        }    }    // page1.cj    @Entry    @Component    class Page1 {        @State var scale1: Float32   = 1.0        @State var opacity1: Float64  = 1.0            func build() {            Column() {                Image(@r(app.media.background)).width(50.percent).height(50.percent)            }            .width(100.percent)            .height(100.percent)            .scale(x: scale1, y: 1.0)            .opacity(opacity1)            .onClick { e =>                Router.push(url: "MyView")            }        }            protected func onTransition(): Unit {            PageTransitionEnter(PageTransitionOptions(duration: 1200, curve: Curve.Linear))            .onEnter { ty: RouteType, progress: Float64 =>                match (ty) {                    case RouteType.Push | RouteType.Pop =>                        scale1 = Float32(progress)                        opacity1 = progress                    case _ => ()                }            }            PageTransitionExit(PageTransitionOptions(duration: 1200, curve: Curve.Ease))            .onExit { ty: RouteType, progress: Float64 =>                match (ty) {                    case RouteType.Push =>                        this.scale1 = Float32(1.0 - progress)                        this.opacity1 = 1.0 - progress                    case _ => ()                }            }        }    }

图片

截屏2024-10-28 09.51.29.png