HarmonyOS Swiper是否有办法分滑动方向分开控制EdgeEffect

在swiper中希望滑动到最顶上元素时,顶上往下拉时没有边缘效果。滑动到最下面元素时,往上拉有边缘效果,要怎么实现?

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

可通过手势拦截增强能力实现,参考示例如下:

// xxx.ets
class MyDataSource0725 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() {
  }
}

@Entry
@Component
struct SwiperExample0725 {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource0725 = new MyDataSource0725([])
  @State controlNumber: number = 0;
  @State needEffect: boolean = true;
  @State curIndex: number = 0;

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

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) => {
          Text(item.toString())
            .width('90%')
            .height(160)
            .backgroundColor(0xAFEEEE)
            .textAlign(TextAlign.Center)
            .fontSize(30)
        }, (item: string) => item)
      }
      .vertical(true)
      .effectMode(EdgeEffect.Spring)
      .cachedCount(2)
      .index($$this.curIndex)
      .autoPlay(false)
      .loop(false)
      .indicatorInteractive(true)
      .itemSpace(0)
      .indicator( // 设置圆点导航点样式
        new DotIndicator()
          .itemWidth(15)
          .itemHeight(15)
          .selectedItemWidth(15)
          .selectedItemHeight(15)
          .color(Color.Gray)
          .selectedColor(Color.Blue))
      .curve(Curve.Linear)
      .onChange((index: number) => {
        console.info(index.toString())
      })
      .onGestureRecognizerJudgeBegin((event: BaseGestureEvent, current: GestureRecognizer,) => {
        if (current.isBuiltIn() && current.getType() == GestureControl.GestureType.PAN_GESTURE) {
          let panEvent = event as PanGestureEvent;
          if (this.curIndex === 0 && panEvent.velocityY > 0) {
            return GestureJudgeResult.REJECT;
          }
        }
        return GestureJudgeResult.CONTINUE;
      })
    }.width('100%').height('100%')
    .margin({ top: 5 })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
onTouch事件是否可以判断滑动方向
2029浏览 • 1回复 待解决
HarmonyOS List滑动速度是否控制
318浏览 • 1回复 待解决
HarmonyOS Swiper滑动相关
5浏览 • 0回复 待解决
HarmonyOS 如何判断滑动方向
34浏览 • 1回复 待解决
如何控制通知是否铃声?
1806浏览 • 1回复 待解决
HarmonyOS 是否滑动验证弹窗组件
80浏览 • 1回复 待解决
微服务下的前后台用户是否分开
2780浏览 • 1回复 待解决
是否闪光灯开关控制能力?
1746浏览 • 1回复 待解决