HarmonyOS onGesture回调函数监听滑动手势事件?

import { GestureControl } from '@ohos.gesture';
.onGesture((event: GestureEvent) => {
  // 处理滑动手势
  if (event.type === GestureType.Swipe && event.direction === SwipeDirection.Up) {
    this.currentVideoIndex = (this.currentVideoIndex - 1 + this.videoListUrl.length) % this.videoListUrl.length;
  } else if (event.type === GestureType.Swipe && event.direction === SwipeDirection.Down) {
    this.currentVideoIndex = (this.currentVideoIndex + 1) % this.videoListUrl.length;
  }
});

这样的引入报错,是这功能取消了嘛,还是什么,我是想做一个视频上下滑动的效果。

阅读 573
1 个回答

视频上下滑动可以使用Swiper和Video实现,其中视频文件在/resource/rawfile/video中,可以参考下以下demo

@Entry
@Component
struct TestVideo {
  private videoValue: Array<string> = ['/resources/rawfile/video/video1.mp4', '/resources/rawfile/video/video2.mp4', '/resources/rawfile/video/video3.mp4',
    '/resources/rawfile/video/video4.mp4']
  build() {
    Column() {
      Swiper() {
        ForEach(this.videoValue, (item:string) => {
          PlayVideo({ item: item })
        }, (item:string) => item)
      }
      .index(1)
      .vertical(true)
      .indicator(false)
      .loop(true)
      .align(Alignment.Center)

    }.height('100%')
    .backgroundColor(Color.Black)
  }
}

@Component
struct PlayVideo {
  @Prop item: string ;
  private videoController: VideoController = new VideoController()

  build() {
    Video({ src: this.item, currentProgressRate: 1, controller: this.videoController })
      .controls(false)
      .objectFit(ImageFit.Contain)
      .loop(true)
      .autoPlay(true)
  }
}