1# 创建轮播(Swiper) 2 3 4[Swiper](../reference/arkui-ts/ts-container-swiper.md)组件提供滑动轮播显示的能力。Swiper本身是一个容器组件,当设置了多个子组件后,可以对这些子组件进行轮播显示。通常,在一些应用首页显示推荐的内容时,需要用到轮播显示的能力。 5 6 7## 布局与约束 8 9Swiper作为一个容器组件,在自身尺寸属性未被设置时,会自动根据子组件的大小设置自身的尺寸。如果开发者对Swiper组件设置了固定的尺寸,则在轮播显示过程中均以该尺寸生效;否则,在轮播过程中,会根据子组件的大小自动调整自身的尺寸。 10 11 12## 循环播放 13 14通过loop属性控制是否循环播放,该属性默认值为true。 15 16当loop为true时,在显示第一页或最后一页时,可以继续往前切换到前一页或者往后切换到后一页。如果loop为false,则在第一页或最后一页时,无法继续向前或者向后切换页面。 17 18- loop为true 19 20```ts 21... 22export let swiperController: SwiperController = new SwiperController() 23... 24Swiper(this.swiperController) { 25 Text("0") 26 .width('90%') 27 .height('100%') 28 .backgroundColor(Color.Gray) 29 .textAlign(TextAlign.Center) 30 .fontSize(30) 31 32 Text("1") 33 .width('90%') 34 .height('100%') 35 .backgroundColor(Color.Green) 36 .textAlign(TextAlign.Center) 37 .fontSize(30) 38 39 Text("2") 40 .width('90%') 41 .height('100%') 42 .backgroundColor(Color.Blue) 43 .textAlign(TextAlign.Center) 44 .fontSize(30) 45} 46.loop(true) 47``` 48 49 50 51- loop为false 52 53```ts 54Swiper(this.swiperController) { 55 Text("0") 56 .width('90%') 57 .height('100%') 58 .backgroundColor(Color.Gray) 59 .textAlign(TextAlign.Center) 60 .fontSize(30) 61 62 Text("1") 63 .width('90%') 64 .height('100%') 65 .backgroundColor(Color.Green) 66 .textAlign(TextAlign.Center) 67 .fontSize(30) 68 69 Text("2") 70 .width('90%') 71 .height('100%') 72 .backgroundColor(Color.Blue) 73 .textAlign(TextAlign.Center) 74 .fontSize(30) 75} 76.loop(false) 77``` 78 79 80 81 82## 自动轮播 83 84Swiper通过设置autoPlay属性,控制是否自动轮播子组件。该属性默认值为false。 85 86autoPlay为true时,会自动切换播放子组件,子组件与子组件之间的播放间隔通过interval属性设置。interval属性默认值为3000,单位毫秒。 87 88```ts 89Swiper(this.swiperController) { 90 Text("0") 91 .width('90%') 92 .height('100%') 93 .backgroundColor(Color.Gray) 94 .textAlign(TextAlign.Center) 95 .fontSize(30) 96 97 Text("1") 98 .width('90%') 99 .height('100%') 100 .backgroundColor(Color.Green) 101 .textAlign(TextAlign.Center) 102 .fontSize(30) 103 104 Text("2") 105 .width('90%') 106 .height('100%') 107 .backgroundColor(Color.Pink) 108 .textAlign(TextAlign.Center) 109 .fontSize(30) 110} 111.loop(true) 112.autoPlay(true) 113.interval(1000) 114``` 115 116 117 118 119## 导航点样式 120 121Swiper提供了默认的导航点样式,导航点默认显示在Swiper下方居中位置,开发者也可以通过indicatorStyle属性自定义导航点的位置和样式。 122 123通过indicatorStyle属性,开发者可以设置导航点相对于Swiper组件上下左右四个方位的位置,同时也可以设置每个导航点的尺寸、颜色、蒙层和被选中导航点的颜色。 124 125- 导航点使用默认样式 126 127```ts 128Swiper(this.swiperController) { 129 Text("0") 130 .width('90%') 131 .height('100%') 132 .backgroundColor(Color.Gray) 133 .textAlign(TextAlign.Center) 134 .fontSize(30) 135 136 Text("1") 137 .width('90%') 138 .height('100%') 139 .backgroundColor(Color.Green) 140 .textAlign(TextAlign.Center) 141 .fontSize(30) 142 143 Text("2") 144 .width('90%') 145 .height('100%') 146 .backgroundColor(Color.Pink) 147 .textAlign(TextAlign.Center) 148 .fontSize(30) 149} 150``` 151 152 153 154- 自定义导航点样式 155 156导航点直径设为30vp,左边距为0,导航点颜色设为红色。 157 158```ts 159let swco:Record<string,number|Color> = {'size':30,'left':0,'color':Color.Red} 160Swiper(this.swiperController) { 161 Text("0") 162 .width('90%') 163 .height('100%') 164 .backgroundColor(Color.Gray) 165 .textAlign(TextAlign.Center) 166 .fontSize(30) 167 168 Text("1") 169 .width('90%') 170 .height('100%') 171 .backgroundColor(Color.Green) 172 .textAlign(TextAlign.Center) 173 .fontSize(30) 174 175 Text("2") 176 .width('90%') 177 .height('100%') 178 .backgroundColor(Color.Pink) 179 .textAlign(TextAlign.Center) 180 .fontSize(30) 181} 182.indicatorStyle(swco) 183``` 184 185 186 187 188## 页面切换方式 189 190Swiper支持手指滑动、点击导航点和通过控制器三种方式切换页面,以下示例展示通过控制器切换页面的方法。 191 192```ts 193@Entry 194@Component 195struct SwiperDemo { 196 private swiperController: SwiperController = new SwiperController(); 197 198 build() { 199 Column({ space: 5 }) { 200 Swiper(this.swiperController) { 201 Text("0") 202 .width(250) 203 .height(250) 204 .backgroundColor(Color.Gray) 205 .textAlign(TextAlign.Center) 206 .fontSize(30) 207 Text("1") 208 .width(250) 209 .height(250) 210 .backgroundColor(Color.Green) 211 .textAlign(TextAlign.Center) 212 .fontSize(30) 213 Text("2") 214 .width(250) 215 .height(250) 216 .backgroundColor(Color.Pink) 217 .textAlign(TextAlign.Center) 218 .fontSize(30) 219 } 220 .indicator(true) 221 222 Row({ space: 12 }) { 223 Button('showNext') 224 .onClick(() => { 225 this.swiperController.showNext(); // 通过controller切换到后一页 226 }) 227 Button('showPrevious') 228 .onClick(() => { 229 this.swiperController.showPrevious(); // 通过controller切换到前一页 230 }) 231 }.margin(5) 232 }.width('100%') 233 .margin({ top: 5 }) 234 } 235} 236``` 237 238 239 240 241## 轮播方向 242 243Swiper支持水平和垂直方向上进行轮播,主要通过vertical属性控制。 244 245当vertical为true时,表示在垂直方向上进行轮播;为false时,表示在水平方向上进行轮播。vertical默认值为false。 246 247 248- 设置水平方向上轮播。 249 250```ts 251Swiper(this.swiperController) { 252 ... 253} 254.indicator(true) 255.vertical(false) 256``` 257 258 259 260 261 262- 设置垂直方向轮播。 263 264```ts 265Swiper(this.swiperController) { 266 ... 267} 268.indicator(true) 269.vertical(true) 270``` 271 272 273 274 275 276## 每页显示多个子页面 277 278Swiper支持在一个页面内同时显示多个子组件,通过[displayCount](../reference/arkui-ts/ts-container-swiper.md#属性)属性设置。 279 280```ts 281Swiper(this.swiperController) { 282 Text("0") 283 .width(250) 284 .height(250) 285 .backgroundColor(Color.Gray) 286 .textAlign(TextAlign.Center) 287 .fontSize(30) 288 Text("1") 289 .width(250) 290 .height(250) 291 .backgroundColor(Color.Green) 292 .textAlign(TextAlign.Center) 293 .fontSize(30) 294 Text("2") 295 .width(250) 296 .height(250) 297 .backgroundColor(Color.Pink) 298 .textAlign(TextAlign.Center) 299 .fontSize(30) 300 Text("3") 301 .width(250) 302 .height(250) 303 .backgroundColor(Color.Blue) 304 .textAlign(TextAlign.Center) 305 .fontSize(30) 306} 307.indicator(true) 308.displayCount(2) 309``` 310 311 312 313## 相关实例 314 315针对Swiper组件开发,有以下相关实例可供参考: 316 317- [电子相册(ArkTS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/ETSUI/ElectronicAlbum) 318 319- [Swiper的使用(ArkTS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/ETSUI/SwiperArkTS)