自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OpenHarmony實(shí)現(xiàn)復(fù)合手勢(shì)識(shí)別

系統(tǒng) OpenHarmony
當(dāng)前OpenHarmony提供的熱區(qū)設(shè)置屬性(responseRegion)只能在不同的觸摸熱區(qū)中觸發(fā)相同的事件,那么如何實(shí)現(xiàn)不同熱區(qū)不同事件呢,本例即以上述視頻播放場(chǎng)景為例進(jìn)行說(shuō)明。

應(yīng)用開(kāi)發(fā)中經(jīng)常需要給同一個(gè)組件劃分不同的觸摸熱區(qū),并且不同熱區(qū)觸發(fā)的操作也不同,比如閱讀應(yīng)用通常包含左右兩個(gè)觸摸熱區(qū),用戶(hù)觸摸左側(cè)觸發(fā)向后翻頁(yè),觸摸右側(cè)觸發(fā)向前翻頁(yè);同樣的,視頻應(yīng)用中,長(zhǎng)按視頻播放器的左側(cè)觸發(fā)快退播放,長(zhǎng)按右側(cè)觸發(fā)快進(jìn)播放等等。 

當(dāng)前OpenHarmony提供的熱區(qū)設(shè)置屬性(responseRegion)只能在不同的觸摸熱區(qū)中觸發(fā)相同的事件,那么如何實(shí)現(xiàn)不同熱區(qū)不同事件呢,本例即以上述視頻播放場(chǎng)景為例進(jìn)行說(shuō)明。

效果呈現(xiàn)

效果說(shuō)明:開(kāi)始時(shí)視頻以正常速度播放,播放到5秒時(shí),長(zhǎng)按播放器右側(cè)觸發(fā)快進(jìn)播放,播放到14秒時(shí)長(zhǎng)按播放器左側(cè)觸發(fā)快退播放。

環(huán)境要求

本例基于以下環(huán)境開(kāi)發(fā),開(kāi)發(fā)者也可以基于其他適配的版本進(jìn)行開(kāi)發(fā):

  • IDE: DevEco Studio 4.0 Release
  • SDK: Ohos_sdk_public 4.0.10.13 (API Version 10 Release)

實(shí)現(xiàn)思路

幾乎組件的所有觸摸事件都會(huì)返回event,通過(guò)返回的event可以獲取到觸摸點(diǎn)的坐標(biāo)位置,那么就可以根據(jù)坐標(biāo)位置為不同的組件區(qū)域添加不同的交互動(dòng)作。如圖:假設(shè)有一個(gè)長(zhǎng)度為200vp的組件,我們希望點(diǎn)擊組件的左側(cè)時(shí)觸發(fā)事件A,點(diǎn)擊組件的右側(cè)時(shí)觸發(fā)事件B,那么就可以通過(guò)觸摸點(diǎn)的坐標(biāo)來(lái)判斷,當(dāng)觸摸點(diǎn)的x坐標(biāo)<=100時(shí),觸發(fā)事件A,反之觸發(fā)事件B。

本例即采用上述思路為Video組件的左右兩側(cè)添加不同的交互動(dòng)作。從而實(shí)現(xiàn)長(zhǎng)按視頻播放器的左側(cè)觸發(fā)后退播放,長(zhǎng)按右側(cè)觸發(fā)快進(jìn)播放。

開(kāi)發(fā)步驟

本例詳細(xì)開(kāi)發(fā)步驟如下,開(kāi)發(fā)步驟中僅展示相關(guān)步驟代碼,全量代碼請(qǐng)參考完整代碼章節(jié)的內(nèi)容。

通過(guò)Video組件創(chuàng)建視頻播放器,并添加觸摸手勢(shì),通過(guò)觸摸控制視頻的播放、暫停。

@Component
export struct VideoPlayer{
  //...
  private isPlaying:boolean = true
  private updateTime: number
  private videoController:VideoController = new VideoController()

  build(){
    // 添加視頻組件
    Video({src:this.videoSrc, controller:this.videoController, previewUri:this.videoPreviewer,currentProgressRate:this.playRate})
      //...
      // 獲取當(dāng)前進(jìn)度條的時(shí)間點(diǎn)
      .onUpdate((e)=>{
        this.updateTime = e.time
      })
      .gesture(
        // 添加觸摸手勢(shì)
        TapGesture({count:1})
          .onAction((event:GestureEvent)=>{
            if (this.isPlaying){
              // 觸摸觸發(fā)播放
              this.videoController.start()
              this.isPlaying = !this.isPlaying
            } else {
              // 再次觸摸觸發(fā)暫停
              this.videoController.pause()
              this.isPlaying =!this.isPlaying
            }
          })
      )
  }
}

為Video組件添加長(zhǎng)按手勢(shì),通過(guò)長(zhǎng)按手勢(shì)觸發(fā)播放的快退和快進(jìn)動(dòng)作。由于觸摸手勢(shì)和長(zhǎng)按手勢(shì)需要互斥,即一次只能觸發(fā)一種手勢(shì),所以通過(guò)GestureGroup來(lái)實(shí)現(xiàn)手勢(shì)的互斥。

.gesture(
  // 通過(guò)GestureGroup的GestureMode.Exclusive參數(shù)控制手勢(shì)互斥
  GestureGroup(GestureMode.Exclusive,
    // 添加觸摸手勢(shì)
    TapGesture({count:1})
      .onAction((event:GestureEvent)=>{
        if (this.isPlaying){
          // 觸摸觸發(fā)播放
          this.videoController.start()
          this.isPlaying = !this.isPlaying
        } else {
          // 再次觸摸觸發(fā)暫停
          this.videoController.pause()
          this.isPlaying =!this.isPlaying
        }
      }),
    // 添加長(zhǎng)按手勢(shì)
    LongPressGesture({repeat:true})
      // 長(zhǎng)按時(shí)觸發(fā)快進(jìn)或快退
      .onAction((event)=>{
        //添加快進(jìn)和快退的邏輯,通過(guò)event獲取手勢(shì)坐標(biāo)進(jìn)行判斷。
      })
      // 長(zhǎng)按結(jié)束后播放速度回歸正常
      .onActionEnd(()=>{
       // 添加回歸正常播放的邏輯
      })
  )
)

補(bǔ)充長(zhǎng)按手勢(shì)中的業(yè)務(wù)邏輯:通過(guò)event獲取到觸摸點(diǎn)的x坐標(biāo):localX,當(dāng)localX>=200時(shí),說(shuō)明觸摸點(diǎn)在組件的右側(cè),觸發(fā)快進(jìn)播放;當(dāng)localX<200時(shí),說(shuō)明觸摸點(diǎn)在左側(cè),觸發(fā)快退播放。當(dāng)觸摸停止時(shí),回歸正常播放速度。

// 添加長(zhǎng)按手勢(shì)
LongPressGesture({repeat:true})
// 長(zhǎng)按時(shí)觸發(fā)快進(jìn)或快退
.onAction((event)=>{
  // 獲取到觸摸點(diǎn)x坐標(biāo)localX,當(dāng)localX>=200時(shí),說(shuō)明觸摸點(diǎn)在組件的右側(cè),觸發(fā)快進(jìn)播放
  if (event.fingerList[0].localX>=200){
    // 播放速度變?yōu)?倍速
    this.playRate = PlaybackSpeed.Speed_Forward_2_00_X
  }
  // 當(dāng)localX<200時(shí),說(shuō)明觸摸點(diǎn)在左側(cè),觸發(fā)快退播放
  if (event.fingerList[0].localX<200){
    if (this.intervalCount===0){
      // 通過(guò)進(jìn)度時(shí)間減小來(lái)達(dá)到快退的目的,通過(guò)setInterval來(lái)控制后退的速度,否則會(huì)連續(xù)觸發(fā)后退,瞬間后退到播放起點(diǎn)
      this.seekBack = setInterval(()=>{
        this.updateTime -= 1
        this.videoController.setCurrentTime(this.updateTime)
      },500)
    }
    this.intervalCount = 1
  }
})
// 長(zhǎng)按結(jié)束后播放速度回歸正常
.onActionEnd(()=>{
  // 播放回歸到1倍速
  this.playRate = PlaybackSpeed.Speed_Forward_1_00_X
  // 清空計(jì)時(shí)器
  clearInterval(this.seekBack)
  this.intervalCount = 0
})

完整代碼

本例完整代碼如下:

說(shuō)明:本例中使用的視頻等資源需要替換為開(kāi)發(fā)者自己的資源。

@Entry
@Component
export struct VideoPlayer{
  private videoSrc:Resource = $rawfile('video_1.mp4')
  private isPlaying:boolean = true
  private updateTime: number = 0
  private videoPreviewer:Resource = $r('app.media.wandering_previewer')
  private videoController:VideoController = new VideoController()
  @State playRate:number = 1
  private seekBack:number = 0
  private intervalCount:number = 0

  build(){
    Column(){
      // 添加視頻組件
      Video({src:this.videoSrc, controller:this.videoController, previewUri:this.videoPreviewer,currentProgressRate:this.playRate})
        .width('100%')
        .height('30%')
        .backgroundColor('#fffff0')
        .controls(true)
        .objectFit(ImageFit.Contain)
        // 獲取當(dāng)前進(jìn)度條的時(shí)間點(diǎn)
        .onUpdate((e)=>{
          this.updateTime = e.time
        })
        .gesture(
          // 通過(guò)GestureGroup的GestureMode.Exclusive參數(shù)控制手勢(shì)互斥
          GestureGroup(GestureMode.Exclusive,
            // 添加觸摸手勢(shì)
            TapGesture({count:1})
              .onAction((event:GestureEvent)=>{
                if (this.isPlaying){
                  // 觸摸觸發(fā)播放
                  this.videoController.start()
                  this.isPlaying = !this.isPlaying
                } else {
                  // 再次觸摸觸發(fā)暫停
                  this.videoController.pause()
                  this.isPlaying =!this.isPlaying
                }
              }),
            // 添加長(zhǎng)按手勢(shì)
            LongPressGesture({repeat:true})
              // 長(zhǎng)按時(shí)觸發(fā)快進(jìn)或快退
              .onAction((event)=>{
                // 獲取到觸摸點(diǎn)x坐標(biāo)localX,當(dāng)localX>=200時(shí),說(shuō)明觸摸點(diǎn)在組件的右側(cè),觸發(fā)快進(jìn)播放
                if (event.fingerList[0].localX>=200){
                  // 播放速度變?yōu)?倍速
                  this.playRate = PlaybackSpeed.Speed_Forward_2_00_X
                }
                // 當(dāng)localX<200時(shí),說(shuō)明觸摸點(diǎn)在左側(cè),觸發(fā)快退播放
                if (event.fingerList[0].localX<200){
                  if (this.intervalCount===0){
                    // 通過(guò)進(jìn)度時(shí)間減小來(lái)達(dá)到快退的目的,通過(guò)setInterval來(lái)控制后退的速度,否則會(huì)連續(xù)觸發(fā)后退,瞬間后退到播放起點(diǎn)
                    this.seekBack = setInterval(()=>{
                      this.updateTime -= 1
                      this.videoController.setCurrentTime(this.updateTime)
                    },500)
                  }
                  this.intervalCount = 1
                }
              })
                // 長(zhǎng)按結(jié)束后播放速度回歸正常
              .onActionEnd(()=>{
                // 播放回歸到1倍速
                this.playRate = PlaybackSpeed.Speed_Forward_1_00_X
                // 清空計(jì)時(shí)器
                clearInterval(this.seekBack)
                this.intervalCount = 0
              })
          )
        )
    }
    .height('100%')
    .width('100%')
  }
}
責(zé)任編輯:姜華 來(lái)源: 鴻蒙開(kāi)發(fā)者社區(qū)
相關(guān)推薦

2022-05-17 12:25:59

物聯(lián)網(wǎng)智能建筑樓宇自控

2013-05-07 17:21:09

ELMOS芯片手勢(shì)識(shí)別

2023-08-08 14:16:07

二維碼開(kāi)發(fā)鴻蒙

2012-02-28 14:07:17

Android觸摸屏手勢(shì)識(shí)別

2014-10-09 10:42:48

iOS手勢(shì)識(shí)別

2023-07-06 08:41:20

TTS?Mac?系統(tǒng)

2022-11-30 07:49:49

交互事件屏幕手勢(shì)識(shí)別

2009-08-27 13:10:54

LINQ from子句

2013-05-21 11:20:37

Android游戲開(kāi)發(fā)View手勢(shì)識(shí)別

2012-08-02 12:02:53

CEVA

2009-08-17 10:26:39

鼠標(biāo)手勢(shì)

2010-01-18 18:50:26

VB.NET鼠標(biāo)手勢(shì)

2014-12-31 16:48:43

Touch touchevent多點(diǎn)觸摸

2024-06-21 08:27:21

ViewViewGroup參數(shù)

2022-05-31 15:27:11

CSS動(dòng)畫(huà)

2022-08-10 16:08:38

鴻蒙CSS

2015-07-22 10:34:59

手勢(shì)密碼源碼

2022-06-20 15:27:00

socket對(duì)話(huà)鴻蒙

2023-08-08 14:31:42

輪播圖鴻蒙

2024-01-11 15:54:55

eTS語(yǔ)言TypeScript應(yīng)用開(kāi)發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)