鸿蒙Harmnoy--轮播组件Swiper详解

东风夜放花千树。更吹落,星如雨。宝马雕车香满路。
凤箫声动,玉壶光转,一夜鱼龙舞。
蛾儿雪柳黄金缕。笑语盈盈暗香去。众里寻他千百度,
蓦然回首,那人却在,灯火阑珊处。

目录

一,介绍

二,循环播放

三,自动轮播

四,导航点样式

五,轮播方向

六,每页显示多个子页面

一,介绍

Swiper组件提供滑动轮播显示的能力。Swiper本身是一个容器组件,当设置了多个子组件后,可以对这些子组件进行轮播显示。

Swiper作为一个容器组件,如果设置了自身尺寸属性,则在轮播显示过程中均以该尺寸生效。如果自身尺寸属性未被设置,则分两种情况:如果设置了prevMargin或者nextMargin属性,则Swiper自身尺寸会跟随其父组件;如果未设置prevMargin或者nextMargin属性,则会自动根据子组件的大小设置自身的尺寸。

二,循环播放

通过loop属性控制是否循环播放,该属性默认值为true。

当loop为true时,在显示第一页或最后一页时,可以继续往前切换到前一页或者往后切换到后一页。如果loop为false,则在第一页或最后一页时,无法继续向前或者向后切换页面。

@Entry
@Component
struct TestPage {
  private swiperController: SwiperController = new SwiperController()

  aboutToAppear() {

  }


  aboutToDisappear() {
    // do nothing
  }

  build() {
    Column() {
      Swiper(this.swiperController) {
        Text('yz1')
          .fontColor(Color.White)
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Black)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz2')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Green)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz3')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Blue)
          .textAlign(TextAlign.Center)
          .fontSize(30)
      }
      .loop(true)
    }
  }
}

loop为false:

@Entry
@Component
struct TestPage {
  private swiperController: SwiperController = new SwiperController()

  aboutToAppear() {

  }


  aboutToDisappear() {
    // do nothing
  }

  build() {
    Column() {
      Swiper(this.swiperController) {
        Text('yz1')
          .fontColor(Color.White)
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Black)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz2')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Green)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz3')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Blue)
          .textAlign(TextAlign.Center)
          .fontSize(30)
      }
      .loop(false)
    }
  }
}

三,自动轮播

Swiper通过设置autoPlay属性,控制是否自动轮播子组件。该属性默认值为false。

autoPlay为true时,会自动切换播放子组件,子组件与子组件之间的播放间隔通过interval属性设置。interval属性默认值为3000,单位毫秒。

@Entry
@Component
struct TestPage {
  private swiperController: SwiperController = new SwiperController()

  aboutToAppear() {

  }


  aboutToDisappear() {
    // do nothing
  }

  build() {
    Column() {
      Swiper(this.swiperController) {
        Text('yz1')
          .fontColor(Color.White)
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Black)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz2')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Green)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz3')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Blue)
          .textAlign(TextAlign.Center)
          .fontSize(30)
      }
      .loop(true)
      .autoPlay(true)
      .interval(1000)
    }
  }
}

四,导航点样式

Swiper提供了默认的导航点样式,导航点默认显示在Swiper下方居中位置,开发者也可以通过indicatorStyle属性自定义导航点的位置和样式。

通过indicatorStyle属性,开发者可以设置导航点相对于Swiper组件上下左右四个方位的位置,同时也可以设置每个导航点的尺寸、颜色、蒙层和被选中导航点的颜色。

@Entry
@Component
struct TestPage {
  private swiperController: SwiperController = new SwiperController()

  aboutToAppear() {

  }


  aboutToDisappear() {
    // do nothing
  }

  build() {
    Column() {
      Swiper(this.swiperController) {
        Text('yz1')
          .fontColor(Color.White)
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Black)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz2')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Green)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz3')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Orange)
          .textAlign(TextAlign.Center)
          .fontSize(30)
      }
      .loop(true)
      .autoPlay(true)
      .interval(1000)
      .indicator(
        Indicator.dot()
          .left(0)
          .itemWidth(15)
          .itemHeight(15)
          .selectedItemWidth(30)
          .selectedItemHeight(15)
          .color(Color.Red)
          .selectedColor(Color.Blue)
      )
    }
  }
}

五,轮播方向

Swiper支持水平和垂直方向上进行轮播,主要通过vertical属性控制。

当vertical为true时,表示在垂直方向上进行轮播;为false时,表示在水平方向上进行轮播。vertical默认值为false。

水平方向的轮播之前已经展示了,下面说下垂直方向的轮播:

@Entry
@Component
struct TestPage {
  private swiperController: SwiperController = new SwiperController()

  aboutToAppear() {

  }


  aboutToDisappear() {
    // do nothing
  }

  build() {
    Column() {
      Swiper(this.swiperController) {
        Text('yz1')
          .fontColor(Color.White)
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Black)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz2')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Green)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz3')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Orange)
          .textAlign(TextAlign.Center)
          .fontSize(30)
      }
      .loop(true)
      .autoPlay(true)
      .interval(1000)
      .indicator(
        Indicator.dot()
          .left(0)
          .itemWidth(15)
          .itemHeight(15)
          .selectedItemWidth(30)
          .selectedItemHeight(15)
          .color(Color.Red)
          .selectedColor(Color.Blue)
      )
      .vertical(true)
    }
  }
}

六,每页显示多个子页面

Swiper支持在一个页面内同时显示多个子组件,通过displaycount设置

@Entry
@Component
struct TestPage {
  private swiperController: SwiperController = new SwiperController()

  aboutToAppear() {

  }


  aboutToDisappear() {
    // do nothing
  }

  build() {
    Column() {
      Swiper(this.swiperController) {
        Text('yz1')
          .fontColor(Color.White)
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Black)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz2')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Green)
          .textAlign(TextAlign.Center)
          .fontSize(30)

        Text('yz3')
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Orange)
          .textAlign(TextAlign.Center)
          .fontSize(30)
      }
      .indicator(
        Indicator.dot()
          .left(0)
          .itemWidth(15)
          .itemHeight(15)
          .selectedItemWidth(30)
          .selectedItemHeight(15)
          .color(Color.Red)
          .selectedColor(Color.Blue)
      )
      .displayCount(3)
    }
  }
}

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁震

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值