W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
手勢(shì)識(shí)別組合,即多種手勢(shì)組合為復(fù)合手勢(shì),支持連續(xù)識(shí)別、并行識(shí)別和互斥識(shí)別。
從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
GestureGroup(mode: GestureMode, ...gesture: GestureType[])
參數(shù)名 | 參數(shù)類型 | 必填 | 默認(rèn)值 | 參數(shù)描述 |
---|---|---|---|---|
mode | 是 | - | 設(shè)置組合手勢(shì)識(shí)別模式。 | |
gesture | 否 | - | 設(shè)置1個(gè)或者多個(gè)基礎(chǔ)手勢(shì)類型時(shí),這些手勢(shì)會(huì)被識(shí)別為組合手勢(shì)。若此參數(shù)不填則組合手勢(shì)識(shí)別功能不生效。 說(shuō)明: 當(dāng)需要為一個(gè)組件同時(shí)添加單擊和雙擊手勢(shì)時(shí),可在組合手勢(shì)中添加兩個(gè)TapGesture,需要雙擊手勢(shì)在前,單擊手勢(shì)在后,否則不生效。 |
名稱 | 描述 |
---|---|
Sequence | 順序識(shí)別,按照手勢(shì)的注冊(cè)順序識(shí)別手勢(shì),直到所有手勢(shì)識(shí)別成功。若有一個(gè)手勢(shì)識(shí)別失敗,后續(xù)手勢(shì)識(shí)別均失敗。 |
Parallel | 并發(fā)識(shí)別,注冊(cè)的手勢(shì)同時(shí)識(shí)別,直到所有手勢(shì)識(shí)別結(jié)束,手勢(shì)識(shí)別互相不影響。 |
Exclusive | 互斥識(shí)別,注冊(cè)的手勢(shì)同時(shí)識(shí)別,若有一個(gè)手勢(shì)識(shí)別成功,則結(jié)束手勢(shì)識(shí)別。 |
- // xxx.ets
- @Entry
- @Component
- struct GestureGroupExample {
- @State count: number = 0
- @State offsetX: number = 0
- @State offsetY: number = 0
- @State positionX: number = 0
- @State positionY: number = 0
- @State borderStyles: BorderStyle = BorderStyle.Solid
- build() {
- Column() {
- Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
- .fontSize(15)
- }
- .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
- .height(150)
- .width(200)
- .padding(20)
- .margin(20)
- .border({ width: 3, style: this.borderStyles })
- .gesture(
- // 以下組合手勢(shì)為順序識(shí)別,當(dāng)長(zhǎng)按手勢(shì)事件未正常觸發(fā)時(shí)則不會(huì)觸發(fā)拖動(dòng)手勢(shì)事件
- GestureGroup(GestureMode.Sequence,
- LongPressGesture({ repeat: true })
- .onAction((event: GestureEvent) => {
- if (event.repeat) {
- this.count++
- }
- console.info('LongPress onAction')
- })
- .onActionEnd(() => {
- console.info('LongPress end')
- }),
- PanGesture()
- .onActionStart(() => {
- this.borderStyles = BorderStyle.Dashed
- console.info('pan start')
- })
- .onActionUpdate((event: GestureEvent) => {
- this.offsetX = this.positionX + event.offsetX
- this.offsetY = this.positionY + event.offsetY
- console.info('pan update')
- })
- .onActionEnd(() => {
- this.positionX = this.offsetX
- this.positionY = this.offsetY
- this.borderStyles = BorderStyle.Solid
- console.info('pan end')
- })
- )
- .onCancel(() => {
- console.info('sequence gesture canceled')
- })
- )
- }
- }
示意圖:
按順序首先觸發(fā)長(zhǎng)按事件:
按順序首先觸發(fā)長(zhǎng)按事件,長(zhǎng)按事件識(shí)別結(jié)束之后,其次觸發(fā)拖動(dòng)事件,向右下方拖動(dòng):
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: