1# 动态手势设置 2 3动态设置组件绑定的手势,支持开发者在属性设置时使用if/else语法。 4 5> **说明:** 6> 7> 从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9## gestureModifier 10 11gestureModifier(modifier: GestureModifier) 12 13动态设置组件绑定的手势。 14 15**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 16 17**系统能力:** SystemCapability.ArkUI.ArkUI.Full 18 19**参数:** 20 21| 参数名 | 类型 | 必填 | 说明 | 22| -------- | --------------------- | ---- | ------------------------------------------------------------ | 23| modifier | [GestureModifier](#gesturemodifier-1) | 是 | 在当前组件上,动态设置组件绑定的手势,支持使用if/else语法。<br/>modifier: 手势修改器,开发者需要自定义class实现GestureModifier接口。 | 24 25## GestureModifier 26 27开发者需要自定义class实现GestureModifier接口。 28 29### applyGesture 30applyGesture(event: UIGestureEvent): void 31 32组件需要绑定的手势。 33 34开发者可根据需要自定义实现这个方法,对组件设置需要绑定的手势,支持使用if/else语法进行动态设置。 35 36**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 37 38**参数**: 39 40| 参数名 | 类型 | 说明 | 41| ------------- | ---------------------------------------- | ---------------------------------------- | 42| event | [UIGestureEvent](./ts-uigestureevent.md#uigestureevent) | UIGestureEvent对象,用于设置组件需要绑定的手势。 | 43 44## 示例 45 46```ts 47// xxx.ets 48class MyButtonModifier implements GestureModifier { 49 supportDoubleTap: boolean = true 50 51 applyGesture(event: UIGestureEvent): void { 52 if (this.supportDoubleTap) { 53 event.addGesture( 54 new TapGestureHandler({ count: 2, fingers: 1 }) 55 .tag("aaa") 56 .onAction((event: GestureEvent) => { 57 console.log("button tap ") 58 }) 59 ) 60 } else { 61 event.addGesture( 62 new PanGestureHandler() 63 .onActionStart(()=>{ 64 console.log("Pan start"); 65 }) 66 ) 67 } 68 } 69} 70 71@Entry 72@Component 73struct Index { 74 @State modifier: MyButtonModifier = new MyButtonModifier() 75 76 build() { 77 Row() { 78 Column() { 79 Column() 80 .gestureModifier(this.modifier) 81 .width(500) 82 .height(500) 83 .backgroundColor(Color.Blue) 84 } 85 .width('100%') 86 } 87 .height('100%') 88 } 89} 90```