ts-container-swiper.md 23.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# Swiper

T
fourth  
tianyu 已提交
3 4
 滑块视图容器,提供子组件滑动轮播显示的能力。 

H
geshi  
HelloCrease 已提交
5
> **说明:**
6
>
Z
zengyawen 已提交
7
> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9

Z
zengyawen 已提交
10
## 子组件
Z
zengyawen 已提交
11 12 13

可以包含子组件。

Y
yamila 已提交
14 15
>  **说明:** 
>
H
HelloCrease 已提交
16
>  子组件类型:系统组件和自定义组件,支持渲染控制类型([if/else](../../quick-start/arkts-rendering-control-ifelse.md)、[ForEach](../../quick-start/arkts-rendering-control-foreach.md)和[LazyForEach](../../quick-start/arkts-rendering-control-lazyforeach.md))。
17 18 19 20
>
>  Swiper子组件的visibility属性设置为None,Swiper的displayMode属性设置为SwiperDisplayMode.AutoLinear或displayCount属性设置为'auto'时,对应子组件在视窗内不占位,但不影响导航点个数。
>
>  Swiper子组件的visibility属性设置为None,或者visibility属性设置为Hidden时,对应子组件不显示,但依然会在视窗内占位。
Y
yamila 已提交
21

Z
zengyawen 已提交
22 23 24

## 接口

L
fix doc  
luoying_ace_admin 已提交
25
Swiper(controller?: SwiperController)
Z
zengyawen 已提交
26

27 28
**参数:** 

H
HelloCrease 已提交
29
| 参数名        | 参数类型                                  | 必填   | 参数描述                 |
T
fourth  
tianyu 已提交
30
| ---------- | ------------------------------------- | ---- | -------------------- |
H
HelloCrease 已提交
31
| controller | [SwiperController](#swipercontroller) | 否    | 给组件绑定一个控制器,用来控制组件翻页。 |
Z
zengyawen 已提交
32 33 34 35


## 属性

T
fourth  
tianyu 已提交
36
除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性,不支持[Menu控制](ts-universal-attributes-menu.md)
H
HelloCrease 已提交
37

H
HelloCrease 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| 名称                                    | 参数类型                                     | 描述                                       |
| ------------------------------------- | ---------------------------------------- | ---------------------------------------- |
| index                                 | number                                   | 设置当前在容器中显示的子组件的索引值。<br/>默认值:0<br/>**说明:** <br/>设置小于0或大于等于子组件数量时,按照默认值0处理。<br />从API version 10开始,该属性支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 |
| autoPlay                              | boolean                                  | 子组件是否自动播放。<br/>默认值:false<br/>**说明:** <br/>loop为false时,自动轮播到最后一页时停止轮播。手势切换后不是最后一页时继续播放。 |
| interval                              | number                                   | 使用自动播放时播放的时间间隔,单位为毫秒。<br/>默认值:3000       |
| indicator<sup>10+</sup>               | [DotIndicator](#dotindicator) \| [DigitIndicator](#digitindicator) \| boolean | 设置可选导航点指示器样式。<br/> \- DotIndicator:圆点指示器样式。<br/> \- DigitIndicator:数字指示器样式。<br/> \- boolean:是否启用导航点指示器。<br/>&nbsp;&nbsp;默认值:true<br/>&nbsp;&nbsp;默认类型:DotIndicator |
| loop                                  | boolean                                  | 是否开启循环。<br>设置为true时表示开启循环,在LazyForEach懒循环加载模式下,加载的组件数量建议大于5个。<br/>默认值:true |
| duration                              | number                                   | 子组件切换的动画时长,单位为毫秒。<br/>默认值:400            |
| vertical                              | boolean                                  | 是否为纵向滑动。<br/>默认值:false                   |
| itemSpace                             | number&nbsp;\|&nbsp;string               | 设置子组件与子组件之间间隙。<br/>默认值:0<br/>**说明:** <br/>不支持设置百分比。 |
| displayMode                           | SwiperDisplayMode                        | 主轴方向上元素排列的模式,优先以displayCount设置的个数显示,displayCount未设置时本属性生效。<br/>默认值:SwiperDisplayMode.Stretch |
| cachedCount<sup>8+</sup>              | number                                   | 设置预加载子组件个数。<br/>默认值:1<br/>**说明:** <br/>cachedCount只在Swiper使用[LazyForEach](../../quick-start/arkts-rendering-control-lazyforeach.md)时才生效。 |
| disableSwipe<sup>8+</sup>             | boolean                                  | 禁用组件滑动切换功能。<br/>默认值:false                |
| curve<sup>8+</sup>                    | [Curve](ts-appendix-enums.md#curve)  \| string | 设置Swiper的动画曲线,默认为淡入淡出曲线,常用曲线参考[Curve枚举说明](ts-appendix-enums.md#curve),也可以通过[插值计算](../apis/js-apis-curve.md)模块提供的接口创建自定义的插值曲线对象。<br/>默认值:Curve.Linear |
| indicatorStyle<sup>(deprecated)</sup> | {<br/>left?:&nbsp;[Length](ts-types.md#length),<br/>top?:&nbsp;[Length](ts-types.md#length),<br/>right?:&nbsp;[Length](ts-types.md#length),<br/>bottom?:&nbsp;[Length](ts-types.md#length),<br/>size?:&nbsp;[Length](ts-types.md#length),<br/>mask?:&nbsp;boolean,<br/>color?:&nbsp;[ResourceColor](ts-types.md),<br/>selectedColor?:&nbsp;[ResourceColor](ts-types.md)<br/>} | 设置导航点样式:<br/>\- left: 设置导航点距离Swiper组件左边的距离。<br/>\- top: 设置导航点距离Swiper组件顶部的距离。<br/>\- right: 设置导航点距离Swiper组件右边的距离。<br/>\- bottom: 设置导航点距离Swiper组件底部的距离。<br/>\- size: 设置导航点的直径,不支持设置百分比。默认值:6vp。<br/>\- mask: 设置是否显示导航点蒙层样式。<br/>\- color: 设置导航点的颜色。<br/>\- selectedColor: 设置选中的导航点的颜色。 <br/>从API version 8开始支持,从API version 10开始不再维护,建议使用[indicator](#indicator10对象说明)代替。 |
53
| displayCount<sup>8+</sup>   | number \| string \| <br />[SwiperAutoFill](#swiperautofill10)<sup>10+</sup> | 设置一页内元素显示个数。<br/>默认值:1<br/>**说明:** <br/>字符串类型仅支持设置为'auto',显示效果同SwiperDisplayMode.AutoLinear。<br/>使用number类型且设置小于等于0时,按默认值1显示。<br/>使用number类型时,子组件按照主轴均分Swiper宽度(减去displayCount-1的itemSpace)的方式进行主轴拉伸(收缩)布局。<br/> 使用SwiperAutoFill类型时,通过设置一个子组件最小宽度值minSize,会根据Swiper当前宽度和minSize值自动计算并更改一页内元素显示个数。当minSize为空或者小于等于0时,Swiper显示1列。 |
H
HelloCrease 已提交
54
| effectMode<sup>8+</sup>               | [EdgeEffect](ts-appendix-enums.md#edgeeffect) | 滑动效果,目前支持的滑动效果参见EdgeEffect的枚举说明。<br/>默认值:EdgeEffect.Spring<br/>**说明:** <br/>控制器接口调用时不生效回弹。 |
55
| displayArrow<sup>10+</sup>            | value:[ArrowStyle](#arrowstyle10) \| boolean,<br/>isHoverShow?: boolean | 设置导航点箭头样式。<br/>-value: 支持设置箭头和底板样式,异常场景使用ArrowStyle对象中的默认值。<br/>\-isHoverShow:设置鼠标悬停时是否显示箭头。<br/>默认值:false<br/>**说明:**<br/>isHoverShow为false时,常驻显示箭头,支持点击翻页。<br/>isHoverShow为true时,只有在鼠标悬停时才会显示箭头,并支持点击翻页。 |
56 57
| nextMargin<sup>10+</sup>    | <br/>[Length](ts-types.md#length)<br/> | 后边距,用于露出后一项的一小部分。<br/>默认值:0<br/>**说明:** <br/>仅当SwiperDisplayMode为STRETCH模式时生效。         |
| prevMargin<sup>10+</sup>    | <br/>[Length](ts-types.md#length)<br/> | 前边距,用于露出前一项的一小部分。<br/>默认值:0<br/>**说明:** <br/>仅当SwiperDisplayMode为STRETCH模式时生效。        |
58 59

## SwiperDisplayMode枚举说明
T
fourth  
tianyu 已提交
60

H
HelloCrease 已提交
61 62
| 名称                                | 描述                                       |
| --------------------------------- | ---------------------------------------- |
63 64
| Stretch<sup>(deprecated)</sup>    | Swiper滑动一页的宽度为Swiper组件自身的宽度。<br>从API version 10开始不再维护,建议使用STRETCH代替。 |
| AutoLinear<sup>(deprecated)</sup> | Swiper滑动一页的宽度为子组件宽度中的最大值。<br>从API version 10开始不再维护,建议使用AUTO_LINEAR代替。 |
H
HelloCrease 已提交
65 66
| STRETCH<sup>10+</sup>             | Swiper滑动一页的宽度为Swiper组件自身的宽度。             |
| AUTO_LINEAR<sup>10+</sup>         | Swiper滑动一页的宽度为视窗内最左侧子组件的宽度。              |
67

68
## SwiperController
Z
zengyawen 已提交
69 70 71

Swiper容器组件的控制器,可以将此对象绑定至Swiper组件,然后通过它控制翻页。

72 73 74 75
### showNext

showNext(): void

Y
yamila 已提交
76
翻至下一页。翻页带动效切换过程,时长通过duration指定。
77 78 79 80 81

### showPrevious

showPrevious(): void

Y
yamila 已提交
82
翻至上一页。翻页带动效切换过程,时长通过duration指定。
83 84

### finishAnimation
Z
zengyawen 已提交
85

86 87 88 89 90 91
finishAnimation(callback?: () => void): void

停止播放动画。

**参数:**

H
HelloCrease 已提交
92 93 94
| 参数名      | 参数类型       | 必填项  | 参数描述     |
| -------- | ---------- | ---- | -------- |
| callback | () => void | 否    | 动画结束的回调。 |
Z
zengyawen 已提交
95

H
HelloCrease 已提交
96
## Indicator<sup>10+</sup>
C
chensi10 已提交
97 98 99

设置导航点距离Swiper组件距离。

H
HelloCrease 已提交
100 101 102 103 104 105
| 参数名    | 参数类型                         | 必填项  | 参数描述                                     |
| ------ | ---------------------------- | ---- | ---------------------------------------- |
| left   | [Length](ts-types.md#length) | 否    | 设置导航点距离Swiper组件左边的距离。<br/>默认值:0<br/>单位:vp |
| top    | [Length](ts-types.md#length) | 否    | 设置导航点距离Swiper组件顶部的距离。<br/>默认值:0<br/>单位:vp |
| right  | [Length](ts-types.md#length) | 否    | 设置导航点距离Swiper组件右边的距离。<br/>默认值:0<br/>单位:vp |
| bottom | [Length](ts-types.md#length) | 否    | 设置导航点距离Swiper组件底部的距离。<br/>默认值:0<br/>单位:vp |
C
chensi10 已提交
106

H
HelloCrease 已提交
107
## DotIndicator<sup>10+</sup>
C
chensi10 已提交
108 109 110

圆点指示器属性及功能继承自Indicator。

H
HelloCrease 已提交
111 112 113 114 115 116 117 118 119
| 参数名                | 参数类型                                     | 必填项  | 参数描述                                     |
| ------------------ | ---------------------------------------- | ---- | ---------------------------------------- |
| itemWidth          | [Length](ts-types.md#length)             | 否    | 设置Swiper组件圆点导航指示器的宽。<br/>默认值:6<br/>单位:vp |
| itemHeight         | [Length](ts-types.md#length)             | 否    | 设置Swiper组件圆点导航指示器的高。<br/>默认值:6<br/>单位:vp |
| selectedItemWidth  | [Length](ts-types.md#length)             | 否    | 设置选中Swiper组件圆点导航指示器的宽。<br/>默认值:6<br/>单位:vp |
| selectedItemHeight | [Length](ts-types.md#length)             | 否    | 设置选中Swiper组件圆点导航指示器的高。<br/>默认值:6<br/>单位:vp |
| mask               | boolean                                  | 否    | 设置是否显示Swiper组件圆点导航指示器的蒙版样式。<br/>默认值:false |
| color              | [ResourceColor](ts-types.md#resourcecolor) | 否    | 设置Swiper组件圆点导航指示器的颜色。<br/>默认值:'\#182431'(10%透明度) |
| selectedColor      | [ResourceColor](ts-types.md#resourcecolor) | 否    | 设置选中Swiper组件圆点导航指示器的颜色。<br/>默认值:'\#007DFF' |
C
chensi10 已提交
120

H
HelloCrease 已提交
121
## DigitIndicator<sup>10+</sup>
C
chensi10 已提交
122 123 124

数字指示器属性及功能继承自Indicator。

H
HelloCrease 已提交
125 126 127 128 129 130
| 参数名               | 参数类型                                     | 必填项  | 参数描述                                     |
| ----------------- | ---------------------------------------- | ---- | ---------------------------------------- |
| fontColor         | [ResourceColor](ts-types.md#resourcecolor) | 否    | 设置Swiper组件数字导航点的字体颜色。<br/>默认值:'\#ff182431' |
| selectedFontColor | [ResourceColor](ts-types.md#resourcecolor) | 否    | 设置选中Swiper组件数字导航点的字体颜色。<br/>默认值:'\#ff182431' |
| digitFont         | {<br/>size?:[Length](ts-types.md#length)<br/>weight?:number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string<br/>} | 否    | 设置Swiper组件数字导航点的字体样式:<br/>\- size:数字导航点指示器的字体大小。<br/>默认值:14vp<br/>\- weight:数字导航点指示器的字重。 |
| selectedDigitFont | {<br/>size?:[Length](ts-types.md#length)<br/>weight?:number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string<br/>} | 否    | 设置选中Swiper组件数字导航点的字体样式:<br/>\- size:数字导航点选中指示器的字体大小。<br/>默认值:14vp<br/>\- weight:数字导航点选中指示器的字重。 |
C
chensi10 已提交
131

H
HelloCrease 已提交
132
## ArrowStyle<sup>10+</sup>
133 134
左右箭头属性。

H
HelloCrease 已提交
135 136 137 138 139 140 141 142
| 参数名              | 参数类型                                     | 必填项  | 参数描述                                     |
| ---------------- | ---------------------------------------- | ---- | ---------------------------------------- |
| isShowBackground | boolean                                  | 否    | 设置箭头底板是否显示。<br/>默认值:false                |
| isSidebarMiddle  | boolean                                  | 否    | 设置箭头显示位置。<br/>默认值:false <br/>默认显示在导航点指示器两侧。 |
| backgroundSize   | [Length](ts-types.md#length)             | 否    | 设置底板大小。<br/>在导航点两侧显示:<br/>默认值:24vp<br/>在组件两侧显示:<br/>默认值:32vp<br/>不支持设置百分比。 |
| backgroundColor  | [ResourceColor](ts-types.md#resourcecolor) | 否    | 设置底板颜色。<br/>在导航点两侧显示:<br/>默认值:\#19182431<br/>在组件两侧显示:<br/>默认值:\#00000000 |
| arrowSize        | [Length](ts-types.md#length)             | 否    | 设置箭头大小。<br/>在导航点两侧显示时:<br/>默认值:18vp<br/>在组件两侧显示时:<br/>默认值:24vp<br/>**说明:**<br/>isShowBackground为true时,arrowSize为backgroundSize的3/4。<br/>不支持设置百分比。 |
| arrowColor       | [ResourceColor](ts-types.md#resourcecolor) | 否    | 设置箭头颜色。<br/>默认值:\#182431                 |
143

144
## SwiperAutoFill<sup>10+</sup>
145

146 147 148 149 150
自适应属性。

| 参数名  | 参数类型             | 必填项 | 参数描述                             |
| ------- | -------------------- | ------ | ------------------------------------ |
| minSize | [VP](ts-types.md#VP) | 是     | 设置元素显示最小宽度。<br/>默认值:0 |
151

Z
zengyawen 已提交
152 153
## 事件

Y
yamila 已提交
154
除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件:
Z
zengyawen 已提交
155

H
HelloCrease 已提交
156 157 158
| 名称                                       | 功能描述                                     |
| ---------------------------------------- | ---------------------------------------- |
| onChange(event: (index: number) => void) | 当前显示的子组件索引变化时触发该事件,返回值为当前显示的子组件的索引值。<br/>-&nbsp;index:当前显示元素的索引。<br/>**说明:** <br>Swiper组件结合LazyForEach使用时,不能在onChange事件里触发子页面UI的刷新。 |
159 160 161
| onAnimationStart<sup>9+</sup>(event: (index: number, targetIndex<sup>10+</sup>: number, extraInfo<sup>10+</sup>: [SwiperAnimationEvent](ts-types.md#swiperanimationevent)) => void) | 切换动画开始时触发该回调。<br/>-&nbsp;index:当前显示元素的索引。<br/>-&nbsp;targetIndex:切换动画目标元素的索引。<br/>-&nbsp;extraInfo:动画相关信息,包括主轴方向上当前显示元素和目标元素相对Swiper起始位置的位移,以及离手速度。<br/>**说明:** <br/>参数为动画开始前的index值(不是最终结束动画的index值),多列Swiper时,index为最左侧组件的索引。 |
| onAnimationEnd<sup>9+</sup>(event: (index: number, extraInfo: [SwiperAnimationEvent](ts-types.md#swiperanimationevent)) => void) | 切换动画结束时触发该回调。<br/>-&nbsp;index:当前显示元素的索引。<br/>-&nbsp;extraInfo:动画相关信息,只返回主轴方向上当前显示元素相对于Swiper起始位置的位移。<br/>**说明:** <br/>当Swiper切换动效结束时触发,包括动画过程中手势中断,通过SwiperController调用finishAnimation。参数为动画结束后的index值,多列Swiper时,index为最左侧组件的索引。 |
| onGestureSwipe<sup>10+</sup>(event: (index: number, extraInfo: [SwiperAnimationEvent](ts-types.md#swiperanimationevent)) => void) | 在页面跟手滑动过程中,逐帧触发该回调。<br/>-&nbsp;index:当前显示元素的索引。<br/>-&nbsp;extraInfo:动画相关信息,只返回主轴方向上当前显示元素相对于Swiper起始位置的位移。<br/>**说明:** <br/>多列Swiper时,index为最左侧组件的索引。 |
Z
zengyawen 已提交
162 163

## 示例
Z
zengyawen 已提交
164

S
sunjiakun 已提交
165
### 示例1
H
geshi  
HelloCrease 已提交
166 167
```ts
// xxx.ets
Z
zengyawen 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
class MyDataSource implements IDataSource {
  private list: number[] = []
  private listener: DataChangeListener

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): any {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    this.listener = listener
  }

  unregisterDataChangeListener() {
  }
}

Z
zengyawen 已提交
192 193 194 195
@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
Z
zengyawen 已提交
196 197
  private data: MyDataSource = new MyDataSource([])

198
  aboutToAppear(): void {
Z
zengyawen 已提交
199 200 201 202 203 204
    let list = []
    for (var i = 1; i <= 10; i++) {
      list.push(i.toString());
    }
    this.data = new MyDataSource(list)
  }
Z
zengyawen 已提交
205 206 207 208

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
Z
zengyawen 已提交
209
        LazyForEach(this.data, (item: string) => {
210
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(30)
Z
zengyawen 已提交
211
        }, item => item)
Z
zengyawen 已提交
212
      }
Z
zengyawen 已提交
213
      .cachedCount(2)
Z
zengyawen 已提交
214 215 216
      .index(1)
      .autoPlay(true)
      .interval(4000)
217 218
      .indicator(true)
      .loop(true)
Z
zengyawen 已提交
219 220
      .duration(1000)
      .itemSpace(0)
221 222 223 224 225 226 227
      .displayArrow({
        isShowBackground:true,
        isSidebarMiddle:true,
        backgroundSize:24,
        backgroundColor:Color.White,
        arrowSize:18,
        arrowColor:Color.Blue},false)
228
      .curve(Curve.Linear)
Z
zengyawen 已提交
229 230 231
      .onChange((index: number) => {
        console.info(index.toString())
      })
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
        console.info("index: " + index)
        console.info("current offset: " + extraInfo.currentOffset)
      })
      .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
        console.info("index: " + index)
        console.info("targetIndex: " + targetIndex)
        console.info("current offset: " + extraInfo.currentOffset)
        console.info("target offset: " + extraInfo.targetOffset)
        console.info("velocity: " + extraInfo.velocity)
      })
      .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
        console.info("index: " + index)
        console.info("current offset: " + extraInfo.currentOffset)
      })
Z
zengyawen 已提交
247

248 249
      Row({ space: 12 }) {
        Button('showNext')
Z
zengyawen 已提交
250 251 252
          .onClick(() => {
            this.swiperController.showNext()
          })
253
        Button('showPrevious')
Z
zengyawen 已提交
254 255 256
          .onClick(() => {
            this.swiperController.showPrevious()
          })
257 258 259
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
Z
zengyawen 已提交
260 261 262
  }
}
```
Y
yamila 已提交
263

264
![swiper](figures/swiper.gif)
S
sunjiakun 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

### 示例2
```ts
// xxx.ets
class MyDataSource implements IDataSource {
  private list: number[] = []
  private listener: DataChangeListener

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): any {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    this.listener = listener
  }

  unregisterDataChangeListener() {
  }
}

@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource = new MyDataSource([])

  aboutToAppear(): void {
    let list = []
    for (var i = 1; i <= 10; i++) {
      list.push(i.toString());
    }
    this.data = new MyDataSource(list)
  }

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) => {
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(30)
        }, item => item)
      }
      .cachedCount(2)
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .indicator(Indicator.dot()
        .itemWidth(15)
        .itemHeight(15)
        .selectedItemWidth(15)
        .selectedItemHeight(15)
        .color(Color.Gray)
        .selectedColor(Color.Blue))
      .loop(true)
      .duration(1000)
      .itemSpace(0)
      .displayArrow(true,true)
      Row({ space: 12 }) {
        Button('showNext')
          .onClick(() => {
            this.swiperController.showNext()
          })
        Button('showPrevious')
          .onClick(() => {
            this.swiperController.showPrevious()
          })
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
  }
}
```
![swiper](figures/swiper-dot.gif)

### 示例3
```ts
// xxx.ets
class MyDataSource implements IDataSource {
  private list: number[] = []
  private listener: DataChangeListener

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): any {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    this.listener = listener
  }

  unregisterDataChangeListener() {
  }
}

@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource = new MyDataSource([])

  aboutToAppear(): void {
    let list = []
    for (var i = 1; i <= 10; i++) {
      list.push(i.toString());
    }
    this.data = new MyDataSource(list)
  }

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) => {
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(30)
        }, item => item)
      }
      .cachedCount(2)
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .indicator(Indicator.digit()
        .right(130)
        .top(200)
        .fontColor(Color.Gray)
        .selectedFontColor(Color.Gray)
        .digitFont({size:20,weight:FontWeight.Bold})
        .selectedDigitFont({size:20,weight:FontWeight.Normal}))
      .loop(true)
      .duration(1000)
      .itemSpace(0)
      .displayArrow(true,false)
      Row({ space: 12 }) {
        Button('showNext')
          .onClick(() => {
            this.swiperController.showNext()
          })
        Button('showPrevious')
          .onClick(() => {
            this.swiperController.showPrevious()
          })
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
  }
}
```
![swiper](figures/swiper-digit.gif)