• 【Harmony OS】【ARK UI】组件内转场api 基本使用


    参考资料 文档中心

    说明

    从 API Version 7 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

    组件转场主要通过transition属性进行配置转场参数,在组件插入和删除时进行过渡动效,主要用于容器组件子组件插入删除时提升用户体验(需要配合animateTo才能生效,动效时长、曲线、延时跟随animateTo中的配置)。

    属性

    名称参数类型默认值参数描述
    transitionObject-所有参数均为可选参数,详细描述见transition入参说明

    transition入参说明

    参数名称参数类型默认值必填参数描述
    typeTransitionTypeAll默认包括组件新增和删除。说明不指定Type时说明插入删除使用同一种效果。
    opacitynumber1设置组件转场时的透明度效果,为插入时起点和删除时终点的值。
    translate{x? : number,y? : number,z? : number}-设置组件转场时的平移效果,为插入时起点和删除时终点的值。
    scale{x? : number,y? : number,z? : number,centerX? : number,centerY? : number}-设置组件转场时的缩放效果,为插入时起点和删除时终点的值
    rotate{x?: number,y?: number,z?: number,angle?: Angle,centerX?: Length,centerY?: Length}-设置组件转场时的旋转效果,为插入时起点和删除时终点的值。

    TransitionType枚举说明

    名称描述
    All指定当前的Transition动效生效在组件的所有变化场景。
    Insert指定当前的Transition动效生效在组件的插入场景。
    Delete指定当前的Transition动效生效在组件的删除场景。

    示例

    示例功能通过一个Button控制第二个Button的出现和消失,并通过transition配置第二个Button出现和消失的过场动画。

    1. @Entry
    2. @Component
    3. struct TransitionExample {
    4. @State btn1: boolean = false
    5. @State show: string = "show"
    6. build() {
    7. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center,}) {
    8. Button(this.show).width(80).height(30).backgroundColor(0x317aff).margin({bottom:50})
    9. .onClick(() => {
    10. animateTo({ duration: 1000 }, () => {
    11. this.btn1 = !this.btn1
    12. if(this.btn1){
    13. this.show = "hide"
    14. }else{
    15. this.show = "show"
    16. }
    17. })
    18. })
    19. if (this.btn1) {
    20. // 插入和删除配置为不同的过渡效果
    21. Button() {
    22. Image($r('app.media.bg1')).width("80%").height(300)
    23. }.transition({ type: TransitionType.Insert, scale : {x:0,y:1.0} })
    24. .transition({ type: TransitionType.Delete, scale: { x: 1.0, y: 0.0 } })
    25. }
    26. }.height(400).width("100%").padding({top:100})
    27. }
    28. }

    a84061a0f872f7585c0b6845a2628c9f_294x510.gif%40900-0-90-f.gif

    补充说明

    scale 参数说

    参数名类型必填默认值说明
    xnumber1x轴的缩放倍数。
    ynumber1y轴的缩放倍数。
    znumber1z轴的缩放倍数。
    centerXnumber | string0变换中心点x轴坐标。
    centerYnumber | string0变换中心点y轴坐标。

    scale : {x:0,y:1.0,centerX:100,centerY:250}描述 从该逐渐从x轴从无到有,y轴没有扩展,从(100,250)点进去扩展

    scale : {x:1,y:0,centerX:100,centerY:250}描述 从该逐渐从x轴没有扩展,y从原有的高度变无 从(100,250)点进去扩展

    translate 简单实用

    示例代码

    1. @Entry
    2. @Component
    3. struct TranslateExample {
    4. @State btn1: boolean = false
    5. @State myText:string="显示"
    6. private onclick(){
    7. animateTo({ duration: 1000 }, () => {
    8. this.btn1 = !this.btn1
    9. if(this.btn1){
    10. this.myText = "隐藏"
    11. }else{
    12. this.myText = "显示"
    13. }
    14. })
    15. }
    16. build() {
    17. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center,}) {
    18. Text(this.myText)
    19. .fontSize(50)
    20. .fontWeight(FontWeight.Bold)
    21. .onClick(this.onclick.bind(this))
    22. if (this.btn1) {
    23. // 插入和删除配置为不同的过渡效果
    24. Button() {
    25. Image($r('app.media.bg1')).width(300).height(300)
    26. }
    27. .transition({ type: TransitionType.Insert, translate : {x:400,y:400,z:100} })
    28. .transition({ type: TransitionType.Delete, translate: { x: 300, y: 200 ,z:230} })
    29. }
    30. }
    31. .width('100%')
    32. .height('100%')
    33. }
    34. }

    运行效果

    Translate.gif

    总结:transition({ type: TransitionType.Insert, translate : {x:400,y:400,z:100} })的意思

    进场的时候从x轴400 y轴400 z轴100 平移到x轴0 y轴 z轴0的位置 (就是自身的位置)

    transition({ type: TransitionType.Delete, translate: { x: 300, y: 200 ,z:230} })的意思

    当type为TransitionType.Delete的时候,退场就是从x轴0y轴0 z周0 平移到 x轴300 y轴300 z轴230的坐标消失

    rotate简单实用

    rotate参数说明

    参数名类型必填默认值说明
    xnumber0旋转向量
    ynumber0旋转向量
    znumber0旋转向量
    centerXnumber | string不填的话默认是该组件的中心点旋转x轴的中心点
    centerYnumber | string不填的话默认是该组件的中心点旋转Y轴的中心点
    anglenumber | string0旋转角度

    示例代码

    1. @Entry
    2. @Component
    3. struct RotateExample {
    4. @State ImageShow: boolean = false//Image 是否显示
    5. @State show: string = "show"
    6. build() {
    7. Column() {
    8. Button(this.show).width(80).height(30).backgroundColor(Color.Red).margin({bottom:50})
    9. .onClick(() => {
    10. animateTo({ duration: 5000 }, () => {
    11. this.ImageShow = !this.ImageShow
    12. if(this.ImageShow){
    13. this.show = "hide"
    14. }else{
    15. this.show = "show"
    16. }
    17. })
    18. })
    19. if (this.ImageShow) {
    20. // 插入和删除配置为不同的过渡效果
    21. Image($r('app.media.icon')).width("80%").height(300)
    22. .transition({ type: TransitionType.Delete,
    23. rotate: { x: 0, y: 0 ,z:1, angle:270,centerX:0,centerY:0} //从(0,0,1)顺时针旋转270度
    24. })
    25. .transition({
    26. type: TransitionType.Insert,
    27. // todo 从(0,0 ,-1) 从逆时针90度开始旋转到原始的位置
    28. rotate : {x: 0, y: 0, z:-1,angle:90 ,centerX:0, centerY:0}
    29. })
    30. }
    31. }.height(400).width("100%").padding({top:100})
    32. }
    33. }

    如下图所示,我们使用是左手,不是右手切记

    cke_40205.png

    效果如下

    %E6%97%8B%E8%BD%AC%E5%8A%A8%E7%94%BB.gif

    总结

    如上图安培定律所示左手大拇指的方向就是z的方向(0,0,-1)安装当前的桌面水平向右为x轴正方向水平向下为y轴正方向,z轴负方向就是大拇指向量,左手大拇指的方式是面向手机屏幕背面的,我们可以发现四指握旋方向是逆时针

    rotate : {x: 0, y: 0, z:-1,angle:90 ,centerX:0, centerY:0} ,其中90度,那就是从顺时针90运动原始的位置

    rotate: { x: 0, y: 0 ,z:1, angle:270,centerX:0,centerY:0} (0,0,1)同样的道理,左手大拇指的方向是面向屏幕的方向的,此时四指握旋方向是顺时针,然后在旋转270后消失

     

     欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    数据库-范式例题
    前端基础5——UI框架Layui
    文件上传过大被限制问题-springboot
    CTF竞赛题目类型
    [三维前缀或]Jobs (Easy Version) 2022牛客多校第4场 D
    Hashing to elliptic curve算法改进
    kafka简单测试以及可视化界面安装
    干货 | JavaScript脚本注入,完成Selenium 无法做到的那些事
    使用 GraphiQL 可视化 GraphQL 架构
    C++ 学习(16)模板 - 函数模板 与 类模板
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126155292