W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
組合手勢(shì)由多種單一手勢(shì)組合而成,通過(guò)在GestureGroup中使用不同的GestureMode來(lái)聲明該組合手勢(shì)的類(lèi)型,支持順序識(shí)別、并行識(shí)別和互斥識(shí)別三種類(lèi)型。
- GestureGroup(mode:GestureMode, ...gesture:GestureType[])
順序識(shí)別組合手勢(shì)對(duì)應(yīng)的GestureMode為Sequence。順序識(shí)別組合手勢(shì)將按照手勢(shì)的注冊(cè)順序識(shí)別手勢(shì),直到所有的手勢(shì)識(shí)別成功。當(dāng)順序識(shí)別組合手勢(shì)中有一個(gè)手勢(shì)識(shí)別失敗時(shí),所有的手勢(shì)識(shí)別失敗。
以一個(gè)由長(zhǎng)按手勢(shì)和拖動(dòng)手勢(shì)組合而成的順序手勢(shì)為例:
在一個(gè)Column組件上綁定了translate屬性,通過(guò)修改該屬性可以設(shè)置組件的位置移動(dòng)。然后在該組件上綁定LongPressGesture和PanGesture組合而成的Sequence組合手勢(shì)。當(dāng)觸發(fā)LongPressGesture時(shí),更新顯示的數(shù)字。當(dāng)長(zhǎng)按后進(jìn)行拖動(dòng)時(shí),根據(jù)拖動(dòng)手勢(shì)的回調(diào)函數(shù),實(shí)現(xiàn)組件的拖動(dòng)。
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State offsetX: number = 0;
- @State offsetY: number = 0;
- @State count: 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(28)
- }
- // 綁定translate屬性可以實(shí)現(xiàn)組件的位置移動(dòng)
- .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
- .height(250)
- .width(300)
- //以下組合手勢(shì)為順序識(shí)別,當(dāng)長(zhǎng)按手勢(shì)事件未正常觸發(fā)時(shí)不會(huì)觸發(fā)拖動(dòng)手勢(shì)事件
- .gesture(
- // 聲明該組合手勢(shì)的類(lèi)型為Sequence類(lèi)型
- GestureGroup(GestureMode.Sequence,
- // 該組合手勢(shì)第一個(gè)觸發(fā)的手勢(shì)為長(zhǎng)按手勢(shì),且長(zhǎng)按手勢(shì)可多次響應(yīng)
- LongPressGesture({ repeat: true })
- // 當(dāng)長(zhǎng)按手勢(shì)識(shí)別成功,增加Text組件上顯示的count次數(shù)
- .onAction((event: GestureEvent) => {
- if (event.repeat) {
- this.count++;
- }
- console.info('LongPress onAction');
- })
- .onActionEnd(() => {
- console.info('LongPress end');
- }),
- // 當(dāng)長(zhǎng)按之后進(jìn)行拖動(dòng),PanGesture手勢(shì)被觸發(fā)
- PanGesture()
- .onActionStart(() => {
- this.borderStyles = BorderStyle.Dashed;
- console.info('pan start');
- })
- // 當(dāng)該手勢(shì)被觸發(fā)時(shí),根據(jù)回調(diào)獲得拖動(dòng)的距離,修改該組件的位移距離從而實(shí)現(xiàn)組件的移動(dòng)
- .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;
- })
- )
- )
- }
- }
拖拽事件是一種典型的順序識(shí)別組合手勢(shì)事件,由長(zhǎng)按手勢(shì)事件和滑動(dòng)手勢(shì)事件組合而成。只有先長(zhǎng)按達(dá)到長(zhǎng)按手勢(shì)事件預(yù)設(shè)置的時(shí)間后進(jìn)行滑動(dòng)才會(huì)觸發(fā)拖拽事件。如果長(zhǎng)按事件未達(dá)到或者長(zhǎng)按后未進(jìn)行滑動(dòng),拖拽事件均識(shí)別失敗。
并行識(shí)別組合手勢(shì)對(duì)應(yīng)的GestureMode為Parallel。并行識(shí)別組合手勢(shì)中注冊(cè)的手勢(shì)將同時(shí)進(jìn)行識(shí)別,直到所有手勢(shì)識(shí)別結(jié)束。并行識(shí)別手勢(shì)組合中的手勢(shì)進(jìn)行識(shí)別時(shí)互不影響。
以在一個(gè)Column組件上綁定點(diǎn)擊手勢(shì)和雙擊手勢(shì)組成的并行識(shí)別手勢(shì)為例,由于單擊手勢(shì)和雙擊手勢(shì)是并行識(shí)別,因此兩個(gè)手勢(shì)可以同時(shí)進(jìn)行識(shí)別,二者互不干涉。
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State count1: number = 0;
- @State count2: number = 0;
- build() {
- Column() {
- Text('parallel gesture\n' + 'tapGesture count is 1:' + this.count1 + '\ntapGesture count is 2:' + this.count2 + '\n')
- .fontSize(28)
- }
- .height(200)
- .width(250)
- // 以下組合手勢(shì)為并行并別,單擊手勢(shì)識(shí)別成功后,若在規(guī)定時(shí)間內(nèi)再次點(diǎn)擊,雙擊手勢(shì)也會(huì)識(shí)別成功
- .gesture(
- GestureGroup(GestureMode.Parallel,
- TapGesture({ count: 1 })
- .onAction(() => {
- this.count1++;
- }),
- TapGesture({ count: 2 })
- .onAction(() => {
- this.count2++;
- })
- )
- )
- }
- }
當(dāng)由單擊手勢(shì)和雙擊手勢(shì)組成一個(gè)并行識(shí)別組合手勢(shì)后,在區(qū)域內(nèi)進(jìn)行點(diǎn)擊時(shí),單擊手勢(shì)和雙擊手勢(shì)將同時(shí)進(jìn)行識(shí)別。
當(dāng)只有單次點(diǎn)擊時(shí),單擊手勢(shì)識(shí)別成功,雙擊手勢(shì)識(shí)別失敗。
當(dāng)有兩次點(diǎn)擊時(shí),若兩次點(diǎn)擊相距時(shí)間在規(guī)定時(shí)間內(nèi)(默認(rèn)規(guī)定時(shí)間為300毫秒),觸發(fā)兩次單擊事件和一次雙擊事件。
當(dāng)有兩次點(diǎn)擊時(shí),若兩次點(diǎn)擊相距時(shí)間超出規(guī)定時(shí)間,觸發(fā)兩次單擊事件不觸發(fā)雙擊事件。
互斥識(shí)別組合手勢(shì)對(duì)應(yīng)的GestureMode為Exclusive?;コ庾R(shí)別組合手勢(shì)中注冊(cè)的手勢(shì)將同時(shí)進(jìn)行識(shí)別,若有一個(gè)手勢(shì)識(shí)別成功,則結(jié)束手勢(shì)識(shí)別,其他所有手勢(shì)識(shí)別失敗。
以在一個(gè)Column組件上綁定單擊手勢(shì)和雙擊手勢(shì)組合而成的互斥識(shí)別組合手勢(shì)為例,由于單擊手勢(shì)只需要一次點(diǎn)擊即可觸發(fā)而雙擊手勢(shì)需要兩次,每次的點(diǎn)擊事件均被單擊手勢(shì)消費(fèi)而不能積累成雙擊手勢(shì),所以雙擊手勢(shì)無(wú)法觸發(fā)。
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State count1: number = 0;
- @State count2: number = 0;
- build() {
- Column() {
- Text('parallel gesture\n' + 'tapGesture count is 1:' + this.count1 + '\ntapGesture count is 2:' + this.count2 + '\n')
- .fontSize(28)
- }
- .height(200)
- .width(250)
- //以下組合手勢(shì)為互斥并別,單擊手勢(shì)識(shí)別成功后,雙擊手勢(shì)會(huì)識(shí)別失敗
- .gesture(
- GestureGroup(GestureMode.Exclusive,
- TapGesture({ count: 1 })
- .onAction(() => {
- this.count1++;
- }),
- TapGesture({ count: 2 })
- .onAction(() => {
- this.count2++;
- })
- )
- )
- }
- }
當(dāng)由單擊手勢(shì)和雙擊手勢(shì)組成一個(gè)互斥識(shí)別組合手勢(shì)后,在區(qū)域內(nèi)進(jìn)行點(diǎn)擊時(shí),單擊手勢(shì)和雙擊手勢(shì)將同時(shí)進(jìn)行識(shí)別。
當(dāng)只有單次點(diǎn)擊時(shí),單擊手勢(shì)識(shí)別成功,雙擊手勢(shì)識(shí)別失敗。
當(dāng)有兩次點(diǎn)擊時(shí),單擊手勢(shì)在第一次點(diǎn)擊時(shí)即宣告識(shí)別成功,此時(shí)雙擊手勢(shì)已經(jīng)失敗。即使在規(guī)定時(shí)間內(nèi)進(jìn)行了第二次點(diǎn)擊,雙擊手勢(shì)事件也不會(huì)進(jìn)行響應(yīng),此時(shí)會(huì)觸發(fā)單擊手勢(shì)事件的第二次識(shí)別成功。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: