HarmonyOS 首页轮播效果实现

HarmonyOS
2024-12-24 15:42:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

参考以下demo:

Index页:

import router from '@ohos.router';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  pageInfos: NavPathStack = new NavPathStack()
  @State message: string = '登录'

  aboutToAppear(){
    if(this.pageInfos === undefined){
      this.pageInfos = new NavPathStack();
    }
    this.pageInfos.pushPath({name :'SwiperPage'},false)
  }

  build() {
    Column() {
      Row({space:100}){
        Button(this.message).width(80).height(40)
          .onClick(() => {
            router.replaceUrl({
              url:'pages/SwiperPage',
              params:true
            }).catch((error:BusinessError) => {
              console.error('Failed to replaceUrl: ' + error);
            })
          })
      }
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

SwiperPage页:

@Entry
@Component
struct SwiperPage {
  private DISPLAY_COUNT: number = 1
  @State currentIndex: number = 0
  @State data: number[] = []
  private swiperWidth: number = 0
  @State imageSrc: string[] = [
    'xxxx',
    'xxxx',
    'xxx'
  ]
  @State backgroundImageSrc: string[] = [
    'xxx',
    'xxx',
    'xxxx'
  ]

  aboutToAppear(): void {
    this.imageSrc.forEach((item, index) => {
      this.data.push(index);
    });
  }

  private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
    let nextIndex = index
    if (index > 0 && event.currentOffset > 0) {
      nextIndex--
    } else if (index < 3 && event.currentOffset < 0) {
      nextIndex++
    }
    let swipeRatio = Math.abs(event.currentOffset / this.swiperWidth)
    let currentIndex = swipeRatio > 0.5 ? nextIndex : index // 页面滑动超过一半,tabBar切换到下一页。
    return { 'index': currentIndex}
  }

  build() {
    Column() {
      Swiper() {
        ForEach(this.imageSrc, (item: string, index: number) => {
          Image(item)
            .width('90%')
            .height('100%')
        }, (item: string) => item)
      }
      .onAreaChange((oldValue: Area,newValue: Area)=> {
        let width = Number.parseFloat(newValue.width.toString())
        this.swiperWidth = Number.isNaN(width) ? 0 : width
      })
      .onChange((index) => {
        this.currentIndex = index
      })
      .margin({top:91.5})
      .height(165.3)
      .indicator(false)
      .loop(true)
      .autoPlay(true)
      .displayCount(this.DISPLAY_COUNT, true)
      .effectMode(EdgeEffect.None)
      .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
        // 切换动画开始时触发该回调。下划线跟着页面一起滑动,同时宽度渐变。
        this.currentIndex = targetIndex
      })
      .onGestureSwipe((index: number,event: TabsAnimationEvent) => {
        // 在页面跟手滑动过程中,逐帧触发该回调。
        let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,event)
        this.currentIndex = currentIndicatorInfo.index
      })
      // 自定义指示器
      Row() {
        ForEach(this.data, (index: number) => {
          Column()
            .width(this.currentIndex === index ? 10 : 7)
            .height(this.currentIndex === index ? 10 : 7)
            .margin(5)
            .borderRadius(this.currentIndex === index ? 10 : 7)
            .backgroundColor(Color.Green)
            .backgroundColor(this.currentIndex === index ? '#8baba8a8' : '#ffffff')
        }, (item: string) => item)
      }
      // 设置指示点距离swiper上下的距离
      .margin({ top: -25 })
    }
    .width('100%')
    .height(400)
    .backgroundImage(this.backgroundImageSrc[this.currentIndex])
    .backgroundImageSize(ImageSize.Cover)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.

module.json5页添加:

"requestPermissions": [
{"name":"ohos.permission.INTERNET"}
]
  • 1.
  • 2.
  • 3.

main_pages.json页修改:

{
  "src": [
  "pages/Index",
  "pages/SwiperPage"
  ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
分享
微博
QQ
微信
回复
2024-12-24 18:48:40


相关问题
HarmonyOS 效果实现方案
1082浏览 • 1回复 待解决
HarmonyOS 动画效果实现
620浏览 • 1回复 待解决
HarmonyOS clipShape 动画效果实现
165浏览 • 0回复 待解决
HarmonyOS 类似翻页效果实现
669浏览 • 1回复 待解决
HarmonyOS list选中效果实现
278浏览 • 1回复 待解决
PopWindow的效果实现有哪些?
965浏览 • 1回复 待解决
HarmonyOS如何实现文字轮播效果
739浏览 • 1回复 待解决
动态布局下加载loading效果实现
1268浏览 • 1回复 待解决
引导遮罩效果实现的最佳方案
1780浏览 • 1回复 待解决
HarmonyOS 消息首页框架实现
610浏览 • 1回复 待解决
HarmonyOS 首页框架问题
818浏览 • 1回复 待解决
HarmonyOS 需要文字上下切换轮播案例
211浏览 • 1回复 待解决
HarmonyOS 如何实现粒子效果
354浏览 • 1回复 待解决