鸿蒙HarmonyOS NEXT开发:动态手势设置(ArkTS通用属性)

110 篇文章 0 订阅

动态手势设置

动态设置组件绑定的手势,支持开发者在属性设置时使用if/else语法。

说明:

从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

gestureModifier

gestureModifier(modifier: GestureModifier)

动态设置组件绑定的手势。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
modifierGestureModifier在当前组件上,动态设置组件绑定的手势,支持使用if/else语法。
modifier: 手势修改器,开发者需要自定义class实现GestureModifier接口。

GestureModifier

开发者需要自定义class实现GestureModifier接口。

applyGesture

applyGesture(event: UIGestureEvent): void

组件需要绑定的手势。

开发者可根据需要自定义实现这个方法,对组件设置需要绑定的手势,支持使用if/else语法进行动态设置。

参数

参数名类型说明
eventUIGestureEventUIGestureEvent对象,用于设置组件需要绑定的手势。

示例

// xxx.ets
class MyButtonModifier implements GestureModifier {
  supportDoubleTap: boolean = true

  applyGesture(event: UIGestureEvent): void {
    if (this.supportDoubleTap) {
      event.addGesture(
        new TapGestureHandler({ count: 2, fingers: 1 })
          .tag("aaa")
          .onAction((event: GestureEvent) => {
            console.log("button tap ")
          })
      )
    } else {
      event.addGesture(
        new PanGestureHandler()
          .onActionStart(()=>{
            console.log("Pan start");
          })
      )
    }
  }
}

@Entry
@Component
struct Index {
  @State modifier: MyButtonModifier = new MyButtonModifier()

  build() {
    Row() {
      Column() {
        Column()
          .gestureModifier(this.modifier)
          .width(500)
          .height(500)
          .backgroundColor(Color.Blue)
      }
      .width('100%')
    }
    .height('100%')
  }
}

 

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值