使用组合手势中的互斥手势GestureMode.Exclusive来实现长按手势LongPressGesture触发弹出bindMenu,点击手势TapGesture触发弹出bindPopup。
@Entry @Component struct GestureGroupDemo { @State isMenu: boolean = false; @State isPopup: boolean = false; @Builder bindMenuBuilder() { Row() { Text('MenuContent'); }.backgroundColor(Color.Pink); } @Builder bindPopupBuilder() { Row() { Text('PopupContent'); }.backgroundColor(Color.Orange); } build() { RelativeContainer() { Button('长按或者点击') .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .fontSize(28) .gesture( GestureGroup(GestureMode.Exclusive, LongPressGesture({ repeat: true }) .onAction(() => { this.isPopup = false; this.isMenu = true; }), TapGesture({ count: 1, fingers: 1 }) .onAction(() => { this.isMenu = false; this.isPopup = !this.isPopup; })) ) .bindContextMenu(this.isMenu, this.bindMenuBuilder()) .bindPopup(this.isPopup, { builder: this.bindPopupBuilder(), onStateChange: (e) => { if (!e.isVisible) { this.isPopup = false; } }, }); } .width('100%') .height('100%'); } }