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

Z
zengyawen 已提交
3

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

Z
zengyawen 已提交
8

E
ester.zhou 已提交
9
The **<Swiper\>** component provides a container that allows users to switch among child components through swiping.
Z
zengyawen 已提交
10 11 12


## Required Permissions
Z
zengyawen 已提交
13 14 15

None

Z
zengyawen 已提交
16 17

## Child Components
Z
zengyawen 已提交
18 19 20

This component can contain child components.

Z
zengyawen 已提交
21 22 23 24 25 26

## APIs

Swiper(value:{controller?: SwiperController})

- Parameters
E
ester.zhou 已提交
27
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
28
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
29
  | controller | [SwiperController](#swipercontroller) | No | null | Controller bound to the component to control the page switching. |
Z
zengyawen 已提交
30 31 32 33


## Attributes

E
esterzhou 已提交
34
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
35
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
36 37 38 39 40 41 42 43 44 45
| index | number | 0 | Index of the child component currently displayed in the container. |
| autoPlay | boolean | false | Whether to enable automatic playback for child component switching. If this attribute is **true**, the indicator dots do not take effect. |
| interval | number | 3000 | Interval for automatic playback, in ms. |
| indicator | boolean | true | Whether to enable the navigation dots. |
| loop | boolean | true | Whether to enable loop playback. |
| duration | number | 400 | Duration of the animation for switching child components, in ms. |
| vertical | boolean | false | Whether vertical swiping is used. |
| itemSpace | Length | 0 | Space between child components. |
| cachedCount<sup>8+</sup> | number | 1 | Number of child components to be cached. |
| disableSwipe<sup>8+</sup> | boolean | false | Whether to disable the swipe feature. |
E
ester.zhou 已提交
46 47
| curve<sup>8+</sup> | [Curve](ts-animatorproperty.md) \| Curves | Curve.Ease | Animation curve. The ease-in/ease-out curve is used by default. For details about common curves, see [Curve enums](ts-animatorproperty.md). You can also create custom curves ([interpolation curve objects](ts-interpolation-calculation.md)) by using APIs provided by the interpolation calculation module. |
| indicatorStyle<sup>8+</sup> | {<br/>left?:&nbsp;Length,<br/>top?:&nbsp;Length,<br/>right?:&nbsp;Length,<br/>bottom?:&nbsp;Length,<br/>size?:&nbsp;Length,<br/>color?:&nbsp;Color,<br/>selectedColor?:&nbsp;Color<br/>} | -          | Style of the navigation dots indicator.<br/>- **left**:&nbsp;distance between the navigation dots indicator and the left edge of the **\<Swiper>** component.<br/>- **top**:&nbsp;distance between the navigation dots indicator and the top edge of the **\<Swiper>** component.<br/>- **right**:&nbsp;distance between the navigation dots indicator and the right edge of the **\<Swiper>** component.<br/>- **bottom**:&nbsp;distance between the navigation dots indicator and the right edge of the **\<Swiper>** component.<br/>- **size**:&nbsp;diameter of the navigation dots indicator.<br/>- **color**:&nbsp;color of the navigation dots indicator.<br/>- **selectedColor**:&nbsp;color of the selected navigation dot. |
Z
zengyawen 已提交
48 49


E
ester.zhou 已提交
50
## SwiperController
Z
zengyawen 已提交
51

E
ester.zhou 已提交
52
Controller of the **<Swiper\>** component. You can bind this object to the **<Swiper\>** component and use it to control page switching.
Z
zengyawen 已提交
53

E
ester.zhou 已提交
54
| Name | Description |
Z
zengyawen 已提交
55
| -------- | -------- |
E
ester.zhou 已提交
56 57
| showNext():void | Turns to the next page. |
| showPrevious():void | Turns to the previous page. |
Z
zengyawen 已提交
58 59 60 61


## Events

E
ester.zhou 已提交
62
| Name | Description |
Z
zengyawen 已提交
63
| -------- | -------- |
E
ester.zhou 已提交
64
| onChange( index: number) =&gt; void | Triggered when the index of the currently displayed component changes. |
Z
zengyawen 已提交
65 66 67 68


## Example

Z
zengyawen 已提交
69

E
ester.zhou 已提交
70 71
```ts
// xxx.ets
Z
zengyawen 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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 已提交
96 97 98 99
@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
Z
zengyawen 已提交
100 101
  private data: MyDataSource = new MyDataSource([])

E
ester.zhou 已提交
102
  aboutToAppear(): void {
Z
zengyawen 已提交
103 104 105 106 107 108
    let list = []
    for (var i = 1; i <= 10; i++) {
      list.push(i.toString());
    }
    this.data = new MyDataSource(list)
  }
Z
zengyawen 已提交
109 110 111 112

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
Z
zengyawen 已提交
113 114 115
        LazyForEach(this.data, (item: string) => {
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(20)
        }, item => item)
Z
zengyawen 已提交
116
      }
Z
zengyawen 已提交
117
      .cachedCount(2)
Z
zengyawen 已提交
118 119 120 121 122 123 124 125
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .indicator(true) // Navigation dots are enabled by default.
      .loop(false) // Loop playback is enabled by default.
      .duration(1000)
      .vertical(false) // Horizontal swiping is enabled by default.
      .itemSpace(0)
Z
zengyawen 已提交
126
      .curve(Curve.Linear) // Animation curve
Z
zengyawen 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      .onChange((index: number) => {
        console.info(index.toString())
      })

      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('next')
          .onClick(() => {
            this.swiperController.showNext()
          })
        Button('preview')
          .onClick(() => {
            this.swiperController.showPrevious()
          })
      }
    }.margin({ top: 5 })
  }
}
```

Z
zengyawen 已提交
146
![en-us_image_0000001211898474](figures/en-us_image_0000001211898474.gif)