#盲盒+码#HarmonyOS应用API手势方法-绑定手势方法 原创

鸿蒙时代
发布于 2022-11-23 15:30
浏览
0收藏

【本文正在参加「盲盒」+码有奖征文活动】(https://ost.51cto.com/posts/19288)
描述:为组件绑定不同类型的手势事件,并设置事件的响应方法。

Api:从API Version 7开始支持

一、绑定手势识别:

通过如下属性给组件绑定手势识别,手势识别成功后可以通过事件回调通知组件。
#盲盒+码#HarmonyOS应用API手势方法-绑定手势方法-鸿蒙开发者社区#盲盒+码#HarmonyOS应用API手势方法-绑定手势方法-鸿蒙开发者社区

响应手势事件:
组件通过手势事件绑定不同GestureType的手势对象,各手势对象提供的事件响应手势操作,提供手势相关信息。下面通过TapGesture对象的onAction事件响应点击事件,获取事件相关信息。其余手势对象的事件定义见各个手势对象章节。 若需绑定多种手势请使用 组合手势。

#盲盒+码#HarmonyOS应用API手势方法-绑定手势方法-鸿蒙开发者社区

多类枚举对象参考:
https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/ts-gesture-settings-0000001430440673-V3#ZH-CN_TOPIC_0000001430440673__绑定手势识别
二、示例代码:

@Entry
@Component
struct GestureSettingsExample {
  @State priorityTestValue: string = '';
  @State parallelTestValue: string = '';
  @State num:number = 0;

  build() {
    Column() {
      Column() {
        Text('TapGesture:' + this.priorityTestValue).fontSize(28)
          .gesture(
          TapGesture()
            .onAction(() => {
              this.priorityTestValue += '\nText';
            }))
      }
      .height(300)
      .width(300)
      .padding(20)
      .margin(20)
      .border({ width: 3 })
      // 设置为priorityGesture时,点击文本会忽略Text组件的TapGesture手势事件,优先识别父组件Column的TapGesture手势事件
      .priorityGesture(
      TapGesture()
        .onAction((event: GestureEvent) => {
          this.num ++
          this.priorityTestValue += '\nColumn' + this.num;
        }), GestureMask.IgnoreInternal)

      Column() {
        Text('TapGesture:' + this.parallelTestValue).fontSize(28)
          .gesture(
          TapGesture()
            .onAction(() => {
              this.num ++
              this.parallelTestValue += '\nText' + this.num;
            }))
      }
      .height(300)
      .width(300)
      .padding(20)
      .margin(20)
      .border({ width: 3 })
      // 设置为parallelGesture时,点击文本会同时触发子组件Text与父组件Column的TapGesture手势事件
      .parallelGesture(
      TapGesture()
        .onAction((event: GestureEvent) => {

          this.parallelTestValue += '\nColumn';
        }), GestureMask.Normal)
    }
  }
}

三、效果图
#盲盒+码#HarmonyOS应用API手势方法-绑定手势方法-鸿蒙开发者社区

四、代码地址
(https://gitee.com/jltfcloudcn/jump_to/tree/master/TapGesture)

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
绑定手势方法.docx 45.53K 2次下载
2
收藏
回复
举报
回复
    相关推荐