HarmonyOS 开发-实现Swiper高度可变化效果

介绍

在很多应用中,swiper组件每一个page的高度是不一致的,所以需要swiper组件下方页面的高度跟着一起变化。

效果图预览

使用说明

向左滑动swiper组件,上方swiper组件高度变高,下方页面随着swiper的变化而平滑的变化。

实现思路

实现方案如下:

  1. 生成四个Swiper页面,来进行左右滑动,GridBuilderFunction是生成Swiper的page。
Swiper() {
  Column() {
    Stack() {
      Text('功能栏')
        .textAlign(TextAlign.Center)
        .margin({ top: $r('app.integer.margin_small'), left: $r('app.integer.default_padding') })
            // swiper第一个page
        this.GridBuilderFunction(this.dataPageOne, Const.GRID_DOUBLE_HEIGHT, Const.GRID_TEMPLATE)
    }
    .alignContent(Alignment.TopStart)
  }

  // swiper第二个page
  this.GridBuilderFunction(this.dataPageTwo, Const.GRID_DOUBLE_HEIGHT, Const.GRID_TEMPLATE)
  // swiper第三个page
  this.GridBuilderFunction(this.dataPageThree, Const.GRID_DOUBLE_HEIGHT, Const.GRID_TEMPLATE)
  // swiper第四个page
  this.GridBuilderFunction(this.dataPageFour, Const.GRID_DOUBLE_HEIGHT, Const.GRID_TEMPLATE)
}
  1. Swiper组件的回调,通过左右滑动的距离来计算对应的上下位置的变化,在页面跟手滑动过程中,逐帧触发onGestureSwipe回调,swiperDistance发生变化。
Swiper{
  ...
}
.onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
  animateTo({
    duration: Const.DURATION,
    curve: Curve.EaseOut,
    playMode: PlayMode.Normal,
    onFinish: () => {
      logger.info('play end');
    }
  }, () => { // 通过左右滑动的距离来计算对应的上下位置的变化
    if (index === 0 && extraInfo.currentOffset < 0) {
      this.swiperDistance = extraInfo.currentOffset / Const.SCROLL_WIDTH * Const.SMALL_FONT_SIZE;
    } else if (index === 1 && extraInfo.currentOffset > 0) {
      this.swiperDistance = extraInfo.currentOffset / Const.SCROLL_WIDTH * Const.SMALL_FONT_SIZE - Const.SMALL_FONT_SIZE;
    } else if (index === 2 && extraInfo.currentOffset < 0) {
      this.swiperDistance = extraInfo.currentOffset / Const.SCROLL_WIDTH * Const.GRID_SINGLE_HEIGHT - Const.SMALL_FONT_SIZE;
    } else if (index === 3 && extraInfo.currentOffset > 0) {
      this.swiperDistance = extraInfo.currentOffset / Const.SCROLL_WIDTH * Const.GRID_SINGLE_HEIGHT - Const.SMALL_FONT_SIZE - Const.GRID_SINGLE_HEIGHT;
    }
  })
})
  1. Swiper组件的回调,用来达到平滑变化的效果,切换动画开始时触发onAnimationStart回调。
Swiper{
  ...
}
.onAnimationStart((_: number, targetIndex: number) => {
  animateTo({
    duration: Const.DURATION,
    curve: Curve.EaseOut,
    playMode: PlayMode.Normal,
    onFinish: () => {
      logger.info('play end');
    }
  }, () => {
    if (targetIndex === 0) {
      this.swiperDistance = 0;
    } else if (targetIndex === 1 || targetIndex === 2) {
      this.swiperDistance = -Const.SMALL_FONT_SIZE;
    } else {
      this.swiperDistance = -Const.SMALL_FONT_SIZE - Const.GRID_SINGLE_HEIGHT;
    }
  })
})

高性能知识点

  1. 本例中swiper组件绑定onGestureSwipe回调,该回调为逐帧回调函数,避免在回调内部使用冗余和耗时操作。
  2. 本例中Text绑定visibility属性方法,进而控制组件的显隐状态,避免组件的频繁创建与销毁,提升性能。

工程结构&模块类型

   swipersmoothvariation                // har包
   |---common
   |   |---CommonConstants.ets          // 提供自定义数据    
   |---pages
   |   |---MainPage.ets                 // 滑动变化效果实现页面

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值