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

Z
zengyawen 已提交
3

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

Z
zengyawen 已提交
7

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


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

None

Z
zengyawen 已提交
15 16

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

This component can contain child components.

Z
zengyawen 已提交
20 21 22 23 24 25

## APIs

Swiper(value:{controller?: SwiperController})

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


## Attributes

E
esterzhou 已提交
33
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
34
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
35 36 37 38 39 40 41 42 43 44
| 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. |
45
| 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. |
Z
zengyawen 已提交
46 47 48 49


### SwiperController

50
Controller of the **<Swiper\>** component. You can bind this object to the **<Swiper\>** component and use it to control page switching.
Z
zengyawen 已提交
51

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


## Events

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


## Example

Z
zengyawen 已提交
67 68

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

  private aboutToAppear(): void {
    let list = []
    for (var i = 1; i <= 10; i++) {
      list.push(i.toString());
    }
    this.data = new MyDataSource(list)
  }
Z
zengyawen 已提交
106 107 108 109

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
Z
zengyawen 已提交
110 111 112
        LazyForEach(this.data, (item: string) => {
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(20)
        }, item => item)
Z
zengyawen 已提交
113
      }
Z
zengyawen 已提交
114
      .cachedCount(2)
Z
zengyawen 已提交
115 116 117 118 119 120 121 122
      .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 已提交
123
      .curve(Curve.Linear) // Animation curve
Z
zengyawen 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      .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 已提交
143
![en-us_image_0000001211898474](figures/en-us_image_0000001211898474.gif)