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

E
ester.zhou 已提交
3
 The **\<Swiper>** component is able to display child components in looping mode.
E
ester.zhou 已提交
4 5

> **NOTE**
E
ester.zhou 已提交
6
>
Z
zengyawen 已提交
7
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## Child Components
Z
zengyawen 已提交
11 12 13

This component can contain child components.

E
ester.zhou 已提交
14 15 16 17
>  **NOTE**
>
>  Built-in components and custom components are allowed, with support for ([if/else](../../quick-start/arkts-rendering-control-ifelse.md), [ForEach](../../quick-start/arkts-rendering-control-foreach.md), and [LazyForEach](../../quick-start/arkts-rendering-control-lazyforeach.md)) rendering control.

Z
zengyawen 已提交
18 19 20

## APIs

E
ester.zhou 已提交
21
Swiper(controller?: SwiperController)
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23
**Parameters**
Z
zengyawen 已提交
24

E
ester.zhou 已提交
25 26
| Name       | Type                          | Mandatory | Description                |
| ---------- | ------------------------------------- | ---- | -------------------- |
E
ester.zhou 已提交
27
| controller | [SwiperController](#swipercontroller) | No  | Controller bound to the component to control the page switching.|
Z
zengyawen 已提交
28 29


E
ester.zhou 已提交
30
## Attributes
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. [Menu control](ts-universal-attributes-menu.md) is not supported.
E
ester.zhou 已提交
33 34 35

| Name                         | Type                              | Description                                      |
| --------------------------- | ---------------------------------------- | ---------------------------------------- |
E
ester.zhou 已提交
36 37
| index                       | number                                   | Index of the child component currently displayed in the container.<br>Default value: **0**<br>**NOTE**<br>If the value is less than 0 or greater than or equal to the number of child components, the default value **0** is used.<br>Since API version 10, this attribute supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables.|
| autoPlay                    | boolean                                  | Whether to enable automatic playback for child component switching.<br>Default value: **false**<br>**NOTE**<br>If **loop** is set to **false**, the playback stops when the last page is displayed. The playback continues when the page is not the last page after a swipe gesture.|
E
ester.zhou 已提交
38
| interval                    | number                                   | Interval for automatic playback, in ms.<br>Default value: **3000**                   |
E
ester.zhou 已提交
39
| indicator<sup>10+</sup>   | [DotIndicator](#dotindicator) \| [DigitIndicator](#digitindicator) \| boolean |  Style of the navigation point indicator.<br> \- **DotIndicator**: dot style.<br> \- **DigitIndicator**: digit style.<br> \- **boolean**: whether to enable the navigation point indicator.<br>  Default value: **true**<br>  Default type: **DotIndicator**|
E
ester.zhou 已提交
40 41 42
| loop                        | boolean                                  | Whether to enable loop playback.<br>The value **true** means to enable loop playback. When LazyForEach is used, it is recommended that the number of the components to load exceed 5.<br>Default value: **true**|
| duration                    | number                                   | Duration of the animation for switching child components, in ms.<br>Default value: **400**                       |
| vertical                    | boolean                                  | Whether vertical swiping is used.<br>Default value: **false**                                |
E
ester.zhou 已提交
43
| itemSpace                   | number \| string          | Space between child components.<br>Default value: **0**<br>**NOTE**<br>This parameter cannot be set in percentage.|
E
ester.zhou 已提交
44
| displayMode                 | SwiperDisplayMode                        | Mode in which elements are displayed along the main axis. This attribute takes effect only when **displayCount** is not set.<br>Default value: **SwiperDisplayMode.Stretch**|
E
ester.zhou 已提交
45
| cachedCount<sup>8+</sup>    | number                                   | Number of child components to be cached.<br>Default value: **1**<br>**NOTE**<br>**cachedCount** has caching optimized. You are advised not to use it together with [LazyForEach](../../quick-start/arkts-rendering-control-lazyforeach.md).|
E
ester.zhou 已提交
46
| disableSwipe<sup>8+</sup>   | boolean                                  | Whether to disable the swipe feature.<br>Default value: **false**                             |
E
esterzhou 已提交
47
| curve<sup>8+</sup>          | [Curve](ts-appendix-enums.md#curve)  \| string | Animation curve. The ease-in/ease-out curve is used by default. For details about common curves, see [Curve](ts-appendix-enums.md#curve). You can also create custom curves (interpolation curve objects) by using the API provided by the [interpolation calculation](../apis/js-apis-curve.md) module.<br>Default value: **Curve.Ease**|
E
ester.zhou 已提交
48
| indicatorStyle<sup>8+</sup> | {<br>left?: [Length](ts-types.md#length),<br>top?: [Length](ts-types.md#length),<br>right?: [Length](ts-types.md#length),<br>bottom?: [Length](ts-types.md#length),<br>size?: [Length](ts-types.md#length),<br>mask?: boolean,<br>color?: [ResourceColor](ts-types.md),<br>selectedColor?: [ResourceColor](ts-types.md)<br>} | Style of the navigation point indicator.<br>\- **left**: distance between the navigation point indicator and the left edge of the **\<Swiper>** component.<br>\- **top**: distance between the navigation point indicator and the top edge of the **\<Swiper>** component.<br>\- **right**: distance between the navigation point indicator and the right edge of the **\<Swiper>** component.<br>\- **bottom**: distance between the navigation point indicator and the bottom edge of the **\<Swiper>** component.<br>\- **size**: diameter of the navigation point indicator.<br>\- **mask**: whether to enable the mask for the navigation point indicator.<br>\- **color**: color of the navigation point indicator.<br>\- **selectedColor**: color of the selected navigation dot.|
E
ester.zhou 已提交
49
| displayCount<sup>8+</sup>   | number \| string                                             | Number of elements to display per page.<br>Default value: **1**<br>**NOTE**<br>If the value is of the string type, it can only be **'auto'**, whose display effect is the same as that of **SwiperDisplayMode.AutoLinear**.<br>If the value is of the number type, child components stretch (shrink) on the main axis after the swiper width [deducting the result of itemSpace x (displayCount – 1)] is evenly distributed among them on the main axis.|
E
ester.zhou 已提交
50 51 52
| effectMode<sup>8+</sup>     | [EdgeEffect](ts-appendix-enums.md#edgeeffect)  | Swipe effect. For details, see **EdgeEffect**.<br>Default value: **EdgeEffect.Spring**<br>**NOTE**<br>The spring effect does not take effect when the controller API is called.|
| nextMargin<sup>10+</sup>    | <br>[Length](ts-types.md#length)<br>| Next margin, used to reveal a small part of the next item.<br>Default value: **0**<br>**NOTE**<br>This parameter is valid only when **SwiperDisplayMode** is set to **STRETCH**. If **cachedCount** is set to a value less than or equal to 0, a small part of the next item is displayed, but child components cannot be loaded.        |
| prevMargin<sup>10+</sup>    | <br>[Length](ts-types.md#length)<br>| Previous margin, used to reveal a small part of the previous item.<br>Default value: **0**<br>**NOTE**<br>This parameter is valid only when **SwiperDisplayMode** is set to **STRETCH**. If **cachedCount** is set to a value less than or equal to 0, a small part of the previous item is displayed, but child components cannot be loaded.       |
E
ester.zhou 已提交
53 54 55 56 57 58

## SwiperDisplayMode

| Name| Description|
| ----------- | ------------------------------------------ |
| Stretch     | The slide width of the **\<Swiper>** component is equal to the width of the component.|
E
ester.zhou 已提交
59
| AutoLinear  | The slide width of the **\<Swiper>** component is equal to that of the child component with the maximum width.|
E
ester.zhou 已提交
60

E
ester.zhou 已提交
61
## SwiperController
Z
zengyawen 已提交
62

E
ester.zhou 已提交
63
Controller of the **\<Swiper>** component. You can bind this object to the **\<Swiper>** component and use it to control page switching.
E
ester.zhou 已提交
64 65 66 67 68

### showNext

showNext(): void

E
ester.zhou 已提交
69
Turns to the next page. Page turning occurs with the animation, whose duration is specified by **duration**.
E
ester.zhou 已提交
70 71 72 73

### showPrevious

showPrevious(): void
Z
zengyawen 已提交
74

E
ester.zhou 已提交
75
Turns to the previous page. Page turning occurs with the animation, whose duration is specified by **duration**.
Z
zengyawen 已提交
76

E
ester.zhou 已提交
77 78 79 80
### finishAnimation

finishAnimation(callback?: () => void): void

E
ester.zhou 已提交
81
Stops an animation.
E
ester.zhou 已提交
82 83 84

**Parameters**

E
ester.zhou 已提交
85
| Name   | Type  | Mandatory.| Description|
E
ester.zhou 已提交
86
| --------- | ---------- | ------ | -------- |
E
esterzhou 已提交
87
| callback  | () => void | No    | Callback invoked when the animation stops.|
Z
zengyawen 已提交
88

E
ester.zhou 已提交
89
## Indicator<sup>10+</sup>
Z
zengyawen 已提交
90

E
ester.zhou 已提交
91
Sets the distance between the navigation point indicator and the **\<Swiper>** component.
Z
zengyawen 已提交
92

E
ester.zhou 已提交
93 94 95 96 97 98
| Name| Type| Mandatory.| Description                            |
| ------ | -------- | ------ | ------------------------------------ |
| left   | [Length](ts-types.md#length)   | No    | Distance between the navigation point indicator and the left edge of the **\<Swiper>** component.|
| top    | [Length](ts-types.md#length)   | No    | Distance between the navigation point indicator and the top edge of the **\<Swiper>** component.|
| right  | [Length](ts-types.md#length)   | No    | Distance between the navigation point indicator and the right edge of the **\<Swiper>** component.|
| bottom | [Length](ts-types.md#length)   | No    | Distance between the navigation point indicator and the bottom edge of the **\<Swiper>** component.|
Z
zengyawen 已提交
99

E
ester.zhou 已提交
100
### DotIndicator
Z
zengyawen 已提交
101

E
ester.zhou 已提交
102 103 104 105 106 107 108 109 110 111 112
Defines the navigation point indicator of the dot style, which inherits attributes and features from **Indicator**.

| Name            | Type     | Mandatory.| Description                                              |
| ------------------ | ------------- | ------ | ------------------------------------------------------ |
| itemWidth          | [Length](ts-types.md#length)        | No    | Width of the navigation point indicator of the dot style.<br>Default value: **6**<br>Unit: vp|
| itemHeight         | [Length](ts-types.md#length)        | No    | Height of the navigation point indicator of the dot style.<br>Default value: **6**<br>Unit: vp|
| selectedItemWidth  | [Length](ts-types.md#length)        | No    | Width of the selected indicator dot.<br>Default value: **6**<br>Unit: vp|
| selectedItemHeight | [Length](ts-types.md#length)        | No    | Height of the selected indicator dot.<br>Default value: **6**<br>Unit: vp|
| mask               | boolean       | No    | Whether to enable the mask for the navigation point indicator of the dot style.<br>Default value: **false**|
| color              | [ResourceColor](ts-types.md#resourcecolor) | No    | Color of the navigation point indicator of the dot style.<br>Default value: **'\#007DFF'**|
| selectedColor      | [ResourceColor](ts-types.md#resourcecolor) | No    | Color of the selected indicator dot.<br>Default value: **'\#182431'** (10% opacity)|
E
ester.zhou 已提交
113

E
ester.zhou 已提交
114 115 116 117 118 119 120 121 122 123 124 125
### DigitIndicator

Defines the navigation point indicator of the digit style, which inherits attributes and features from **Indicator**.

| Name           | Type                                                    | Mandatory.| Description                                                    |
| ----------------- | ------------------------------------------------------------ | ------ | ------------------------------------------------------------ |
| fontColor         | [ResourceColor](ts-types.md#resourcecolor)                   | No    | Font color of the navigation point indicator of the digit style.<br>Default value: **'\#ff182431'**|
| selectedFontColor | [ResourceColor](ts-types.md#resourcecolor)                   | No    | Font color of the selected indicator digit.<br>Default value: **'\#ff182431'**|
| digitFont         | {<br>size?:[Length](ts-types.md#length)<br>weight?:number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string<br>} | No    | Font style of the navigation point indicator of the digit style.<br>\- **size**: font size.<br>Default value: **14vp**<br>\- **weight**: font weight.|
| selectedDigitFont | {<br>size?:[Length](ts-types.md#length)<br>weight?:number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string<br>} | No    | Font style of the selected indicator digit.<br>\- **size**: font size.<br>Default value: **14vp**<br>\- **weight**: font weight.|

## Events
E
ester.zhou 已提交
126

E
ester.zhou 已提交
127
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
E
ester.zhou 已提交
128

E
ester.zhou 已提交
129 130 131 132 133
| Name                                                        | Description                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| onChange(event: (index: number) => void)                     | Triggered when the index of the currently displayed child component changes.<br>- **index**: index of the currently displayed element.<br>**NOTE**<br>When the **\<Swiper>** component is used together with **LazyForEach**, the subpage UI update cannot be triggered in the **onChange** event.|
| onAnimationStart<sup>9+</sup>(event: (index: number) => void) | Triggered when the switching animation starts.<br>- **index**: index of the currently displayed element.<br>**NOTE**<br>The **index** parameter indicates the index before the animation starts (not the one after). When the **\<Swiper>** component contains multiple columns, the index is of the leftmost element.|
| onAnimationEnd<sup>9+</sup>(event: (index: number) => void)  | Triggered when the switching animation ends.<br>- **index**: index of the currently displayed element.<br>**NOTE**<br>This event is triggered when the animation ends regardless of whether it is due to a user gesture or invocation of **finishAnimation** through **SwiperController**. The **index** parameter indicates the index after the animation ends. When the **\<Swiper>** component contains multiple columns, the index is of the leftmost element.|
E
ester.zhou 已提交
134 135

## Example
Z
zengyawen 已提交
136

E
ester.zhou 已提交
137 138
```ts
// xxx.ets
Z
zengyawen 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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 已提交
163 164 165 166
@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
Z
zengyawen 已提交
167 168
  private data: MyDataSource = new MyDataSource([])

E
ester.zhou 已提交
169
  aboutToAppear(): void {
Z
zengyawen 已提交
170 171 172 173 174 175
    let list = []
    for (var i = 1; i <= 10; i++) {
      list.push(i.toString());
    }
    this.data = new MyDataSource(list)
  }
Z
zengyawen 已提交
176 177 178 179

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
Z
zengyawen 已提交
180
        LazyForEach(this.data, (item: string) => {
E
esterzhou 已提交
181
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(30)
Z
zengyawen 已提交
182
        }, item => item)
Z
zengyawen 已提交
183
      }
Z
zengyawen 已提交
184
      .cachedCount(2)
Z
zengyawen 已提交
185 186 187
      .index(1)
      .autoPlay(true)
      .interval(4000)
E
esterzhou 已提交
188 189
      .indicator(true)
      .loop(true)
Z
zengyawen 已提交
190 191
      .duration(1000)
      .itemSpace(0)
E
esterzhou 已提交
192
      .curve(Curve.Linear)
Z
zengyawen 已提交
193 194 195 196
      .onChange((index: number) => {
        console.info(index.toString())
      })

E
esterzhou 已提交
197 198
      Row({ space: 12 }) {
        Button('showNext')
Z
zengyawen 已提交
199 200 201
          .onClick(() => {
            this.swiperController.showNext()
          })
E
esterzhou 已提交
202
        Button('showPrevious')
Z
zengyawen 已提交
203 204 205
          .onClick(() => {
            this.swiperController.showPrevious()
          })
E
esterzhou 已提交
206 207 208
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
Z
zengyawen 已提交
209 210 211 212
  }
}
```

E
esterzhou 已提交
213
![swiper](figures/swiper.gif)