1# 组合手势 2 3 4组合手势由多种单一手势组合而成,通过在GestureGroup中使用不同的GestureMode来声明该组合手势的类型,支持[顺序识别](#顺序识别)、[并行识别](#并行识别)和[互斥识别](#互斥识别)三种类型。 5 6 7 8```ts 9GestureGroup(mode:GestureMode, gesture:GestureType[]) 10``` 11 12 13- mode:为GestureMode枚举类。用于声明该组合手势的类型。 14 15- gesture:由多个手势组合而成的数组。用于声明组合成该组合手势的各个手势。 16 17 18## 顺序识别 19 20顺序识别组合手势对应的GestureMode为Sequence。顺序识别组合手势将按照手势的注册顺序识别手势,直到所有的手势识别成功。当顺序识别组合手势中有一个手势识别失败时,所有的手势识别失败。顺序识别手势组仅有最后一个手势可以响应onActionEnd。 21 22以一个由长按手势和拖动手势组合而成的连续手势为例: 23 24在一个Column组件上绑定了translate属性,通过修改该属性可以设置组件的位置移动。然后在该组件上绑定LongPressGesture和PanGesture组合而成的Sequence组合手势。当触发LongPressGesture时,更新显示的数字。当长按后进行拖动时,根据拖动手势的回调函数,实现组件的拖动。 25 26 27 28```ts 29// xxx.ets 30@Entry 31@Component 32struct Index { 33 @State offsetX: number = 0; 34 @State offsetY: number = 0; 35 @State count: number = 0; 36 @State positionX: number = 0; 37 @State positionY: number = 0; 38 @State borderStyles: BorderStyle = BorderStyle.Solid 39 40 build() { 41 Column() { 42 Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY) 43 .fontSize(28) 44 } 45 // 绑定translate属性可以实现组件的位置移动 46 .translate({ x: this.offsetX, y: this.offsetY, z: 0 }) 47 .height(250) 48 .width(300) 49 //以下组合手势为顺序识别,当长按手势事件未正常触发时不会触发拖动手势事件 50 .gesture( 51 // 声明该组合手势的类型为Sequence类型 52 GestureGroup(GestureMode.Sequence, 53 // 该组合手势第一个触发的手势为长按手势,且长按手势可多次响应 54 LongPressGesture({ repeat: true }) 55 // 当长按手势识别成功,增加Text组件上显示的count次数 56 .onAction((event: GestureEvent|undefined) => { 57 if(event){ 58 if (event.repeat) { 59 this.count++; 60 } 61 } 62 console.info('LongPress onAction'); 63 }) 64 .onActionEnd(() => { 65 console.info('LongPress end'); 66 }), 67 // 当长按之后进行拖动,PanGesture手势被触发 68 PanGesture() 69 .onActionStart(() => { 70 this.borderStyles = BorderStyle.Dashed; 71 console.info('pan start'); 72 }) 73 // 当该手势被触发时,根据回调获得拖动的距离,修改该组件的位移距离从而实现组件的移动 74 .onActionUpdate((event: GestureEvent|undefined) => { 75 if(event){ 76 this.offsetX = this.positionX + event.offsetX; 77 this.offsetY = this.positionY + event.offsetY; 78 } 79 console.info('pan update'); 80 }) 81 .onActionEnd(() => { 82 this.positionX = this.offsetX; 83 this.positionY = this.offsetY; 84 this.borderStyles = BorderStyle.Solid; 85 }) 86 ) 87 ) 88 } 89} 90``` 91 92 93 94 95 96>**说明:** 97> 98>拖拽事件是一种典型的顺序识别组合手势事件,由长按手势事件和滑动手势事件组合而成。只有先长按达到长按手势事件预设置的时间后进行滑动才会触发拖拽事件。如果长按事件未达到或者长按后未进行滑动,拖拽事件均识别失败。 99 100 101## 并行识别 102 103并行识别组合手势对应的GestureMode为Parallel。并行识别组合手势中注册的手势将同时进行识别,直到所有手势识别结束。并行识别手势组合中的手势进行识别时互不影响。 104 105以在一个Column组件上绑定点击手势和双击手势组成的并行识别手势为例,由于单击手势和双击手势是并行识别,因此两个手势可以同时进行识别,二者互不干涉。 106 107 108 109```ts 110// xxx.ets 111@Entry 112@Component 113struct Index { 114 @State count1: number = 0; 115 @State count2: number = 0; 116 117 build() { 118 Column() { 119 Text('parallel gesture\n' + 'tapGesture count is 1:' + this.count1 + '\ntapGesture count is 2:' + this.count2 + '\n') 120 .fontSize(28) 121 } 122 .height(200) 123 .width(250) 124 // 以下组合手势为并行并别,单击手势识别成功后,若在规定时间内再次点击,双击手势也会识别成功 125 .gesture( 126 GestureGroup(GestureMode.Parallel, 127 TapGesture({ count: 1 }) 128 .onAction(() => { 129 this.count1++; 130 }), 131 TapGesture({ count: 2 }) 132 .onAction(() => { 133 this.count2++; 134 }) 135 ) 136 ) 137 } 138} 139``` 140 141 142 143 144 145>**说明:** 146> 147>当由单击手势和双击手势组成一个并行识别组合手势后,在区域内进行点击时,单击手势和双击手势将同时进行识别。 148> 149>当只有单次点击时,单击手势识别成功,双击手势识别失败。 150> 151>当有两次点击时,若两次点击相距时间在规定时间内(默认规定时间为300毫秒),触发两次单击事件和一次双击事件。 152> 153>当有两次点击时,若两次点击相距时间超出规定时间,触发两次单击事件不触发双击事件。 154 155 156## 互斥识别 157 158互斥识别组合手势对应的GestureMode为Exclusive。互斥识别组合手势中注册的手势将同时进行识别,若有一个手势识别成功,则结束手势识别,其他所有手势识别失败。 159 160以在一个Column组件上绑定单击手势和双击手势组合而成的互斥识别组合手势为例,由于单击手势只需要一次点击即可触发而双击手势需要两次,每次的点击事件均被单击手势消费而不能积累成双击手势,所以双击手势无法触发。 161 162 163 164```ts 165// xxx.ets 166@Entry 167@Component 168struct Index { 169 @State count1: number = 0; 170 @State count2: number = 0; 171 172 build() { 173 Column() { 174 Text('parallel gesture\n' + 'tapGesture count is 1:' + this.count1 + '\ntapGesture count is 2:' + this.count2 + '\n') 175 .fontSize(28) 176 } 177 .height(200) 178 .width(250) 179 //以下组合手势为互斥并别,单击手势识别成功后,双击手势会识别失败 180 .gesture( 181 GestureGroup(GestureMode.Exclusive, 182 TapGesture({ count: 1 }) 183 .onAction(() => { 184 this.count1++; 185 }), 186 TapGesture({ count: 2 }) 187 .onAction(() => { 188 this.count2++; 189 }) 190 ) 191 ) 192 } 193} 194``` 195 196 197 198 199 200>**说明:** 201> 202>当由单击手势和双击手势组成一个互斥识别组合手势后,在区域内进行点击时,单击手势和双击手势将同时进行识别。 203> 204>当只有单次点击时,单击手势识别成功,双击手势识别失败。 205> 206>当有两次点击时,单击手势在第一次点击时即宣告识别成功,此时双击手势已经失败。即使在规定时间内进行了第二次点击,双击手势事件也不会进行响应,此时会触发单击手势事件的第二次识别成功。 207