HarmonyOS 一屏多页,即使是2页也可以滑动

没有类似的库,一屏多页,即使是2页也可以滑动

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

可参考DEMO:

class MyDataSource implements IDataSource {
  private list: string[] = []
  constructor(list: string[]) {
    this.list = list
  }
  totalCount(): number {
    return this.list.length
  }
  getData(index: number): string {
    return this.list[index]
  }
  registerDataChangeListener(listener: DataChangeListener): void {
  }
  unregisterDataChangeListener() {
  }
}

const MAX_SCALE = 1 // 最大缩放
const MIN_SCALE = 0.85 // 最小缩放
// 可通过以下两个参数变化观察效果,然后优化
const PAGE_DURATION = 200 // 子控件动画时长
const SWIPER_DURATION = 300 // swiper组件切换动画时长
// 拖动时用来计算缩放,影响拖动缩放速度,可根据屏幕尺寸来定
const DRAGGING_MAX_DISTANCE = 1000

@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource = new MyDataSource([])
  @State currentIndex: number = 0
  @State scaleArray: number[] = [];
  startSwiperOffset: number = 0

  aboutToAppear(): void {
    let list: string[] = []
    for (let i = 0; i <= 5; i++) {
      list.push(i.toString());
      this.scaleArray.push(i == 0 ? MAX_SCALE : MIN_SCALE)
    }
    this.data = new MyDataSource(list)
  }build() {
  Column({ space: 25 }) {
    Swiper(this.swiperController) {
      LazyForEach(this.data, (item: string, index: number) => {
        Column() {
          Text(item)
            .width(40)
            .height(40)
            .textAlign(TextAlign.Center)
            .fontSize(30)
        }
        .width("100%")
        .height('100%')
        .backgroundColor(0xFFFF00)
        .scale({ x: this.scaleArray[index], y: this.scaleArray[index] })
        .animation({
          duration:PAGE_DURATION,
          curve:Curve.Linear
        })
      }, (item: string, index: number) => item)
    }
    .displayMode(SwiperDisplayMode.STRETCH)
    .displayCount(1)
    .width('100%')
    .height('100%')
    .index(this.currentIndex) //状态变量
    .cachedCount(2)
    .indicator(true)
    .autoPlay(true)
    .loop(true)
    .duration(SWIPER_DURATION)
    .itemSpace(0)
    .nextMargin(100)
    .prevMargin(100)
    .curve(Curve.Linear)
    .backgroundColor(0x999999)
    .interval(1000)
    .onChange((index: number) => {
      this.currentIndex = index
      this.scaleArray[this.currentIndex] = MAX_SCALE;
      if (this.currentIndex == 0) {
        this.scaleArray[this.scaleArray.length - 1] = MIN_SCALE
      } else
        this.scaleArray[this.currentIndex -1] = MIN_SCALE
      if (this.currentIndex == this.scaleArray.length - 1) {
        this.scaleArray[0] = MIN_SCALE
      } else
        this.scaleArray[this.currentIndex + 1] = MIN_SCALE
    })
    .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
      if (this.startSwiperOffset == 0) this.startSwiperOffset = extraInfo.currentOffset;
      let offset: number = extraInfo.currentOffset
      let currentScale: number = this.scaleArray[index]
      let nextIndex = (index == this.scaleArray.length - 1 ? 0 : index + 1)
      let preIndex = (index == 0 ? this.scaleArray.length - 1 : index - 1)
      let nextScale: number = this.scaleArray[nextIndex]
      let preScale: number = this.scaleArray[preIndex]

      // 滑动距离
      let distance = Math.abs(this.startSwiperOffset - offset)
      currentScale = MAX_SCALE - Math.min(distance / DRAGGING_MAX_DISTANCE, MAX_SCALE - MIN_SCALE)
      if(this.startSwiperOffset > offset) {
        nextScale = MIN_SCALE + Math.min(distance / DRAGGING_MAX_DISTANCE, MAX_SCALE - MIN_SCALE)
        preScale = MIN_SCALE
      } else {
        preScale = MIN_SCALE + Math.min(distance / DRAGGING_MAX_DISTANCE, MAX_SCALE - MIN_SCALE)
        nextScale = MIN_SCALE
      }
      this.scaleArray[this.currentIndex] = currentScale
      this.scaleArray[nextIndex] = nextScale
      this.scaleArray[preIndex] = preScale
    })
    .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
      if(index == targetIndex) {
        let nextIndex = (index == this.scaleArray.length - 1 ? 0 : index + 1)
        let preIndex = (index == 0 ? this.scaleArray.length - 1 : index - 1)
        this.scaleArray[index] = MAX_SCALE
        this.scaleArray[nextIndex] = MIN_SCALE
        this.scaleArray[preIndex] = MIN_SCALE
      } else {
        let nextIndex = (targetIndex == this.scaleArray.length - 1 ? 0 : targetIndex + 1)
        let preIndex = (targetIndex == 0 ? this.scaleArray.length - 1 : targetIndex - 1)
        this.scaleArray[targetIndex] = MAX_SCALE
        this.scaleArray[nextIndex] = MIN_SCALE
        this.scaleArray[preIndex] = MIN_SCALE
      }
    })
    .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
      this.startSwiperOffset = 0
    })
  }
  .width('100%')
  .height(150)
  .margin({ top:150 })
}
}

nextmargin/prevmargin中任意一个大于子组件测算的宽度,nextmargin和prevmargin均不显示

可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-swiper-V5#ZH-CN_TOPIC_0000001884917770__nextmargin10

分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 滑动效果应该如何做
30浏览 • 1回复 待解决
HarmonyOS 卡片添加到负一屏
453浏览 • 1回复 待解决
鸿蒙闪的实现怎么实现?
4750浏览 • 1回复 待解决
如何使H5面适配设备?
771浏览 • 1回复 待解决
跳转设置返回存在白屏
1757浏览 • 1回复 待解决
HarmonyOS 怎么控制web返回上
551浏览 • 1回复 待解决