IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> ArkTS的手势处理(部分) -> 正文阅读

[游戏开发]ArkTS的手势处理(部分)

说些废话

????官方文档:手势处理(基于ArkTS的声明式开发范式)
????我只写了几个,而且我是一个工程测试一个手势,所以就只贴一下.ets的代码了,注释也没写而且都是用的API 8(FA),创建的华为鸿蒙工程。
????那个按速度来识别的SwipeGesture(滑动手势)和以距离来识别的PanGesture(拖动手势)很像,因为我只写了滑动手势,我觉得滑动手势好钝好难识别上。。。

环境

????HUAWEI MatePad Pro 10.8英寸 2019款 HarmonyOS3.0.0.163
????DevEco Studio 3.1 Canary1
????SDK 8
????我看的《API参考》更新时间为2022-12-16 17:46

代码

PinchGesture(捏合手势)

index.ets

import prompt from '@system.prompt';
@Entry
@Component
struct Index {
  //捏合形成的比例
  @State scaleValue: number = 1;
  //图片的宽高
  @State image_width: number = 100;
  @State image_height: number = 100;

  build() {
    Row() {
      Column() {
        Image($r('app.media.windmill'))
          //最大就变成300叭
          .width(this.image_width)
          .height(this.image_height)
      }
      .width('100%')
    }
    .height('100%')
    .gesture(
      PinchGesture({ fingers: 2 })
        .onActionStart((event: GestureEvent) => {
//          prompt.showToast({
//            message: 'onActionStart'
//          })
        })
        .onActionUpdate((event: GestureEvent) => {
          this.image_width *= event.scale
          if(this.image_width > 300){
            this.image_width = 300
          }
          if(this.image_width < 100){
            this.image_width = 100
          }
          this.image_height = this.image_width
//          prompt.showToast({
//            message: 'event = ' + JSON.stringify(event)
//          })
        })
        .onActionEnd(() => {
//          prompt.showToast({
//            message: 'onActionEnd'
//          })
        })
    )
  }
}

展示

图1

RotationGesture(旋转手势)

index.ets

@Entry
@Component
struct Index {
  @State image_angle: number = 0
  //0度和360度的时候z是1
  build() {
    Row() {
      Column() {
        Image($r('app.media.windmill'))
          .width(200)
          .height(200)
          .rotate({
              // 组件以(0, 0, 1)为旋转轴,中心点顺时针旋转angle度
              x: 0,
              y: 0,
              //有bug,转完之后再去转的时候,又是以最初的图片位置开始转的
              z: 1,
              angle: this.image_angle,
              centerX: '50%',
              centerY: '50%'
            }
          )
      }
      .width('100%')
    }
    .height('100%')
    .gesture(
      RotationGesture({fingers: 2})
        .onActionStart((event: GestureEvent) => {
        })
        .onActionUpdate((event: GestureEvent) => {
          this.image_angle = event.angle;
        })
        .onActionEnd(() => {
        })
    )
  }
}

展示

图2

SwipeGesture(滑动手势)

index.ets

import prompt from '@system.prompt';
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = 'Page1'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .gesture(
      SwipeGesture({
        //触发滑动手势的滑动方向为水平方向。
        direction: SwipeDirection.Horizontal,
        fingers: 1,
        speed: 50
      })
        //滑动手势识别成功回调。
        .onAction(event => {
          //angle是负的就是从左往右滑
          if(event.angle > 0){
            //从右往左滑
            featureAbility.startAbility({
              want: {
                deviceId: '',
                bundleName: 'com.openvalley.cyj.gesture.swipe',
                abilityName: 'com.openvalley.cyj.gesture.swipe.MainAbility',
                parameters: {
                  url: 'pages/show'
                }
              }
            })
          }else{
            //从左往右滑
            prompt.showToast({
              message: '当前已经是第一页。'
            })
          }
        })
    )
    .height('100%')
  }
}

show.ets

import prompt from '@system.prompt';
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Show {
  @State message: string = 'Page2'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .gesture(
    SwipeGesture({
      //触发滑动手势的滑动方向为水平方向。
      direction: SwipeDirection.Horizontal,
      fingers: 1,
      speed: 50
    })
      //滑动手势识别成功回调。
      .onAction(event => {
        //angle是负的就是从左往右滑
        if(event.angle > 0){
          //从右往左滑
          featureAbility.startAbility({
            want: {
              deviceId: '',
              bundleName: 'com.openvalley.cyj.gesture.swipe',
              abilityName: 'com.openvalley.cyj.gesture.swipe.MainAbility',
              parameters: {
                url: 'pages/page'
              }
            }
          })
        }else{
          //从左往右滑
          featureAbility.startAbility({
            want: {
              deviceId: '',
              bundleName: 'com.openvalley.cyj.gesture.swipe',
              abilityName: 'com.openvalley.cyj.gesture.swipe.MainAbility',
              parameters: {
                url: 'pages/index'
              }
            }
          })
        }
      })
    )
    .height('100%')
  }
}

page.ets

import prompt from '@system.prompt';
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Page {
  @State message: string = 'Page3'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .gesture(
    SwipeGesture({
      //触发滑动手势的滑动方向为水平方向。
      direction: SwipeDirection.Horizontal,
      fingers: 1,
      speed: 50
    })
      //滑动手势识别成功回调。
      .onAction(event => {
        //angle是负的就是从左往右滑
        if(event.angle > 0){
          //从右往左滑
          prompt.showToast({
            message: '当前已经是最后一页。'
          })
        }else{
          //从左往右滑
          featureAbility.startAbility({
            want: {
              deviceId: '',
              bundleName: 'com.openvalley.cyj.gesture.swipe',
              abilityName: 'com.openvalley.cyj.gesture.swipe.MainAbility',
              parameters: {
                url: 'pages/show'
              }
            }
          })
        }
      })
    )
    .height('100%')
  }
}

展示

????怎么这么难识别上 = =
图3

GestureGroup(组合手势)

????我这里写(cv)的是旋转+捏合。

index.ets

@Entry
@Component
struct Index {
  //捏合形成的比例
  @State scaleValue: number = 1;
  //图片的宽高
  @State image_width: number = 100;
  @State image_height: number = 100;
  //旋转的角度
  @State image_angle: number = 0
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Image($r('app.media.windmill'))
          .width(this.image_width)
          .height(this.image_height)
          .rotate({
            // 组件以(0, 0, 1)为旋转轴,中心点顺时针旋转angle度
            x: 0,
            y: 0,
            //有bug,转完之后再去转的时候,又是以最初的图片位置开始转的
            z: 1,
            angle: this.image_angle,
            centerX: '50%',
            centerY: '50%'
          }
          )
      }
      .width('100%')
    }
    .gesture(
      GestureGroup(
        //并发识别,注册的手势同时识别,直到所有手势识别结束,手势识别互相不影响。
        GestureMode.Parallel,
        RotationGesture({fingers: 2})
          .onActionUpdate((event: GestureEvent) => {
            this.image_angle = event.angle;
          })
        , PinchGesture({ fingers: 2 })
          .onActionUpdate((event: GestureEvent) => {
            this.image_width *= event.scale
            if(this.image_width > 500){
              this.image_width = 500
            }
            if(this.image_width < 100){
              this.image_width = 100
            }
            this.image_height = this.image_width
          })
      )
    )
    .height('100%')
  }
}

展示

图4

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-12-25 11:39:00  更:2022-12-25 11:39:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/7 19:46:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码