HarmonyOS Next 之轮播图开发指南(Swiper组件)

案例效果

实现一个支持自动播放、手动滑动、分页指示器的图片轮播组件

实现步骤

1. 创建基本布局结构

javascript 复制代码
// 示例代码
@Entry
@Component
struct SwiperExample {
  @State currentIndex: number = 0
  private swiperController: SwiperController = new SwiperController()

  // 轮播图数据源
  @State swiperData: string[] = [
    "/common/images/banner1.jpg",
    "/common/images/banner2.jpg",
    "/common/images/banner3.jpg"
  ]

  build() {
    Column() {
      // Swiper容器
      Swiper(this.swiperController) {
        ForEach(this.swiperData, (item: string) => {
          Image(item)
            .width('100%')
            .height(200)
            .objectFit(ImageFit.Cover)
        })
      }
      .width('100%')
      .height(200)
      .loop(true)  // 循环播放
      .autoPlay(true)  // 自动播放
      .interval(3000)  // 3秒间隔
      .indicator(true)  // 显示分页指示器
      .onChange((index: number) => {
        this.currentIndex = index
      })
    }
  }
}

2. 自定义分页指示器

javascript 复制代码
// 在Column中添加自定义指示器
Row() {
  ForEach(this.swiperData, (item: string, index: number) => {
    Circle()
      .width(8)
      .height(8)
      .fill(index === this.currentIndex ? Color.Blue : Color.Gray)
      .margin(5)
  })
}
.margin({ top: 10 })
.justifyContent(FlexAlign.Center)

3. 添加控制按钮(可选)

javascript 复制代码
Row() {
  Button('上一张')
    .onClick(() => {
      this.swiperController.showPrevious()
    })
  
  Button('下一张')
    .onClick(() => {
      this.swiperController.showNext()
    })
}
.padding(10)

完整示例代码

javascript 复制代码
@Entry
@Component
struct BannerSwiper {
  @State currentIndex: number = 0
  private swiperController: SwiperController = new SwiperController()

  @State swiperData: Resource[] = [
    $r('app.media.banner1'),
    $r('app.media.banner2'),
    $r('app.media.banner3')
  ]

  build() {
    Column() {
      // 轮播图主体
      Swiper(this.swiperController) {
        ForEach(this.swiperData, (item: Resource) => {
          Image(item)
            .width('100%')
            .height(240)
            .borderRadius(8)
            .objectFit(ImageFit.Cover)
        })
      }
      .width('90%')
      .height(240)
      .loop(true)
      .autoPlay(true)
      .interval(3000)
      .indicator(false)
      .onChange((index: number) => {
        this.currentIndex = index
      }

      // 自定义指示器
      Row() {
        ForEach(this.swiperData, (item: Resource, index: number) => {
          Circle()
            .width(index === this.currentIndex ? 12 : 8)
            .height(8)
            .fill(index === this.currentIndex ? '#007DFF' : '#33000000')
            .margin(4)
            .animation({ duration: 200, curve: Curve.EaseInOut })
        })
      }
      .margin({ top: 12 })
    }
    .width('100%')
    .padding(16)
  }
}
相关推荐
前端不太难6 小时前
从单页面到系统化:鸿蒙 App 演进路径
华为·状态模式·harmonyos
想你依然心痛8 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“文思智脑“——PC端AI智能体沉浸式智能写作工作台
人工智能·ar·harmonyos·ai写作
小雨青年8 小时前
鸿蒙 HarmonyOS 6 | Pura X Max 鸿蒙原生适配 09:展开态列表增加字段但不变复杂
华为·harmonyos
richard_yuu8 小时前
鸿蒙治愈游戏模块实战|四大轻量解压游戏、ArkTS动画交互与低功耗落地
游戏·交互·harmonyos
阿钱真强道12 小时前
24 鸿蒙LiteOS GPIO中断实战:从原理到上升沿/下降沿详解
harmonyos·中断·rk·liteos·开源鸿蒙·瑞芯微·rk2206
cd_9492172114 小时前
鸿蒙系统下抖音存储空间不足怎么办?缓存清理教程
缓存·华为·harmonyos
轻口味17 小时前
HarmonyOS 6.1 全栈实战录 - 14 渲染树透镜:FrameNode 渲染状态感知与高性能 UI 调优实战
ui·华为·harmonyos
HwJack2017 小时前
HarmonyOS NEXT 游戏APP开发中如何正确拦截退出手势
游戏·华为·harmonyos
HwJack2017 小时前
HarmonyOS APP开发中ArkTS/JS 类型错误全景拆解
javascript·华为·harmonyos
lqj_本人18 小时前
鸿蒙PC:鸿蒙版本 Electron 框架环境搭建并且实现 XH 笔记应用
笔记·electron·harmonyos