亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

HarmonyOS API 15 Swiper動效模式SwiperAnimationMode應用展示

 滑块视图容器Swiper,提供子组件滑动轮播显示的能力。本节演示了API 15新增的动效模式SwiperAnimationMode的应用展示。



Swiper组件翻页至指定页面的动效模式。描述如下:


  1. 卡片能力: 从API version 15开始,该接口支持在ArkTS卡片中使用。

  2. 元服务API: 从API version 15开始,该接口支持在元服务中使用。

  3. 系统能力: SystemCapability.ArkUI.ArkUI.Full



安装新版的DevEco Studio 5.0.5 Release


新版的DevEco Studio包含了最新的API 17,可以更好的体验鸿蒙的开发乐趣。


https://img1.sycdn.imooc.com/7bb761680958998408310496.jpg





创建一个SwiperAnimationMode示例


创建一个名为“ArkTSSwiperAnimationMode”的项目,用于演示SwiperAnimationMode的使用示例。


https://img1.sycdn.imooc.com/cbf253680958998408300554.jpg


https://img1.sycdn.imooc.com/a63c62680958998408310555.jpg



创建数据源类MyDataSource

该类主要是用于提供Swiper组件的数据源。代码如下



class MyDataSource implements IDataSource {
  private list: number[] = [];

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

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

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

  registerDataChangeListener(listener: DataChangeListener): void {
  }

  unregisterDataChangeListener() {
  }
}





创建Swiper组件


Swiper组件代码如下:


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

  aboutToAppear(): void {
    let list: number[] = [];
    for (let i = 0; i <= 20; i++) {
      list.push(i);
    }
    this.data = new MyDataSource(list);
  }

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) => {
          Text(item.toString())
            .width('90%')
            .height(360)
            .backgroundColor(0xAFEEEE)
            .textAlign(TextAlign.Center)
            .fontSize(30)
        }, (item: string) => item)
      }
      .cachedCount(2)
      .index(1)
      .autoPlay(false)
      .interval(4000)
      .loop(false)
      .duration(1000)
      .itemSpace(5)
      .prevMargin(35)
      .nextMargin(35)
      .onChange((index: number) => {
        console.info(index.toString());
      })
      .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);
      })

      Column({ space: 5 }) {
        Button('NO_ANIMATION 0')
          .onClick(() => {
            this.swiperController.changeIndex(0, SwiperAnimationMode.NO_ANIMATION);
          })
        Button('DEFAULT_ANIMATION 10')
          .onClick(() => {
            this.swiperController.changeIndex(10, SwiperAnimationMode.DEFAULT_ANIMATION);
          })
        Button('FAST_ANIMATION 20')
          .onClick(() => {
            this.swiperController.changeIndex(20, SwiperAnimationMode.FAST_ANIMATION);
          })
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
  }
}




这里重点介绍API 15新增的事件changeIndex(index: number, animationMode?: SwiperAnimationMode | boolean),其中SwiperAnimationMode就是设置翻页至指定页面时的动效模式。默认值:SwiperAnimationMode.NO_ANIMATION。其中,动效模式有以下三种选择:

  1. NO_ANIMATION 无动效翻页至指定页面。

  2. DEFAULT_ANIMATION 有动效翻页至指定页面。

  3. FAST_ANIMATION 先无动效翻页至指定页面附近,再有动效翻页至指定页面。



上述示例,通过三个按钮的点击,来触发不同的动效模式的效果。


NO_ANIMATION 无动效翻页至指定页面,效果如下。



https://img1.sycdn.imooc.com/6e022e680a58998403860809.jpg


DEFAULT_ANIMATION 有动效翻页至指定页面,效果如下。


https://img1.sycdn.imooc.com/eb91dc680a58998503860809.jpg


FAST_ANIMATION 先无动效翻页至指定页面附近,再有动效翻页至指定页面,效果如下。


https://img1.sycdn.imooc.com/cb48ed680a58998503860809.jpg


源码


本示例源码归档至《跟老卫学HarmonyOS开发》:https://github.com/waylau/harmonyos-tutorial


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
全棧工程師
手記
粉絲
1.7萬
獲贊與收藏
2192

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消