鸿蒙学习手册(HarmonyOSNext_API16)_应用开发UI设计:Swiper

HarmonyOS Swiper 轮播组件详解

Swiper 组件是 HarmonyOS 中用于实现滑动轮播的核心工具,适用于首页推荐、图片展示等场景。以下从基础使用高阶功能的全面解析:


一、基础轮播搭建

场景:快速创建一个包含3个页面的水平轮播。

typescript 复制代码
Swiper() {  
  Text('页面1')  
    .width('90%')  
    .height('100%')  
    .backgroundColor(Color.Gray)  
  Text('页面2')  
    .width('90%')  
    .height('100%')  
    .backgroundColor(Color.Green)  
  Text('页面3')  
    .width('90%')  
    .height('100%')  
    .backgroundColor(Color.Pink)  
}  
.loop(true) // 开启循环  
.indicator(true) // 显示默认导航点  
.autoPlay(true) // 自动播放  
.interval(3000) // 3秒切换  

效果

  • 自动循环播放,底部显示导航点。
  • 每3秒切换页面,支持手动滑动。

二、核心功能配置
1. 循环播放 (loop)
  • 默认开启 (loop=true):滑到最后一页可继续滑动到第一页。
  • 关闭循环 (loop=false):滑到边缘时无法继续滑动。

适用场景

  • 商品详情页图片展示 → 关闭循环(避免用户误滑出范围)。
  • 广告轮播 → 开启循环(无限滚动提升曝光)。

2. 自动播放 (autoPlay)
  • 开启autoPlay(true) + interval(毫秒)
  • 暂停:用户手动滑动时自动暂停,需代码手动重启。

优化技巧

typescript 复制代码
.autoPlay(true)  
.interval(5000) // 5秒切换  
.onTouch(event => {  
  if (event.type === TouchType.Down) {  
    // 用户触摸时暂停自动播放  
    swiperController.stopAutoPlay();  
  }  
})  

3. 导航点定制 (indicator)

默认样式

  • 居中显示小圆点,选中态高亮。

自定义样式

typescript 复制代码
.indicator(  
  Indicator.dot()  
    .left(20) // 左侧偏移20vp  
    .itemWidth(10) // 默认点宽度  
    .itemHeight(10)  
    .selectedItemWidth(20) // 选中点宽度  
    .selectedItemHeight(10)  
    .color(Color.Gray) // 默认颜色  
    .selectedColor(Color.Red) // 选中颜色  
)  

箭头显示

typescript 复制代码
.displayArrow({  
  showBackground: true, // 显示箭头背景板  
  arrowSize: 24, // 箭头大小  
  arrowColor: Color.Blue,  
  backgroundSize: 40, // 背景板尺寸  
  backgroundColor: Color.White  
})  

4. 轮播方向 (vertical)
  • 水平轮播 (默认):.vertical(false)
  • 垂直轮播.vertical(true)

适用场景

  • 竖向长图展示(如服装搭配)。
  • 新闻客户端垂直翻页阅读。

5. 多页面同屏 (displayCount)

显示2个子页面

typescript 复制代码
Swiper() {  
  // 添加4个子组件  
}  
.displayCount(2) // 每屏显示2个  

效果

  • 左右滑动时,每次切换2个页面。
  • 导航点数量自动适配(如4页 → 2个导航点)。

三、高阶功能:自定义动画

场景:实现卡片3D翻转效果。

typescript 复制代码
.customContentTransition({  
  timeout: 1000, // 动画时长  
  transition: (proxy: SwiperContentTransitionProxy) => {  
    // 动态修改透明度、缩放、位移  
    this.opacityList[proxy.index] = 1 - Math.abs(proxy.position);  
    this.scaleList[proxy.index] = 1 - Math.abs(proxy.position) * 0.2;  
    this.translateList[proxy.index] = proxy.position * 100;  
  }  
})  

参数解析

  • proxy.position:当前页面滑动进度(-1到1)。
  • proxy.mainAxisLength:轮播主轴长度(水平为宽度,垂直为高度)。

四、性能优化与避坑指南
1. 预加载机制
typescript 复制代码
Swiper()  
  .cachedCount(2) // 预加载前后2页  
  • 原理:提前加载相邻页面,减少滑动卡顿。
2. 动态数据加载
  • 使用 ForEach 动态渲染子组件:
typescript 复制代码
ForEach(this.imageList, (item: ImageData) => {  
  Image(item.url)  
    .width('100%')  
    .height('100%')  
})  
3. 常见问题
  • 导航点错位 :确保子组件数量与 displayCount 匹配。
  • 自动播放失效 :检查是否在触摸事件中调用了 stopAutoPlay() 但未重启。

五、完整案例:带控制器的轮播
typescript 复制代码
@Entry  
@Component  
struct ControlledSwiper {  
  private swiperController: SwiperController = new SwiperController();  

  build() {  
    Column() {  
      Swiper(this.swiperController) {  
        // 添加子组件  
      }  
      Row() {  
        Button('上一页').onClick(() => this.swiperController.showPrevious())  
        Button('下一页').onClick(() => this.swiperController.showNext())  
        Button('跳转至第3页').onClick(() => this.swiperController.changeIndex(2))  
      }  
    }  
  }  
}  

总结

Swiper 组件通过灵活配置可实现:

  • 基础轮播 → 广告展示
  • 多页同屏 → 商品分类
  • 垂直滚动 → 长文阅读
  • 自定义动画 → 特色交互

掌握其核心属性和性能优化技巧,可轻松应对复杂场景需求。

相关推荐
shlR1 小时前
Figma 新手教程学习笔记
笔记·学习·figma
.小墨迹2 小时前
Apollo学习——键盘控制速度
linux·开发语言·c++·python·学习·计算机外设
honey ball2 小时前
正点原子T80烙铁拆解学习
学习
Brookty2 小时前
【MySQL】基础知识
后端·学习·mysql
陵易居士2 小时前
JVM-类加载子系统
jvm·笔记·学习
水水沝淼㵘2 小时前
嵌入式开发学习日志(数据结构--双链表)Day21
c语言·数据结构·学习·算法·排序算法
xiaohanbao093 小时前
day26 Python 自定义函数
开发语言·python·学习·机器学习·信息可视化·numpy
卓应3 小时前
2025年5月华为H12-821新增题库带解析
网络·华为·智能路由器
狮智先生3 小时前
【学习笔记】点云自动化聚类简要总结
笔记·学习·自动化
_waylau3 小时前
华为2024年报:鸿蒙生态正在取得历史性突破
华为·开源·harmonyos