应用程序的手势操作是指在移动设备上使用手指或手势进行与应用程序交互的方式。手势操作可以包括点击、滑动、双击、捏合等动作,用于实现不同的功能和操作。
HarmonyOS中常见的手势操作及其功能:
组合手势是由多个手势组合而成的手势动作。通过不同手势的组合,可以完成更复杂的操作。例如,可以通过组合手势来实现缩放、旋转、滑动等操作。组合手势可以提高用户交互的灵活性和效率。
GestureGroup(mode:GestureMode, ...gesture:GestureType[])
组合手势的顺序识别是指识别由多个手势组合而成的特定顺序的手势。在手势识别中,有些任务可能需要用户按照特定的顺序执行一系列手势才能触发某种操作或功能。例如,可以定义一个组合手势,要求用户首先做一个向左滑动,然后再做一个向上滑动,最后做一个点击动作才能执行某项操作。
组合手势的顺序识别可以应用于许多领域,如移动设备上的手势控制、虚拟现实、游戏等。它提供了更复杂和精确的用户交互方式,使得用户能够通过简单的手势组合来完成更多的操作或者控制。
// xxx.ets@Entry@Componentstruct Index { @State offsetX: number = 0; @State offsetY: number = 0; @State count: number = 0; @State positionX: number = 0; @State positionY: number = 0; @State borderStyles: BorderStyle = BorderStyle.Solid build() { Column() { Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY) .fontSize(28) } // 绑定translate属性可以实现组件的位置移动 .translate({ x: this.offsetX, y: this.offsetY, z: 0 }) .height(250) .width(300) //以下组合手势为顺序识别,当长按手势事件未正常触发时不会触发拖动手势事件 .gesture( // 声明该组合手势的类型为Sequence类型 GestureGroup(GestureMode.Sequence, // 该组合手势第一个触发的手势为长按手势,且长按手势可多次响应 LongPressGesture({ repeat: true }) // 当长按手势识别成功,增加Text组件上显示的count次数 .onAction((event: GestureEvent) => { if (event.repeat) { this.count++; } console.info('LongPress onAction'); }) .onActionEnd(() => { console.info('LongPress end'); }), // 当长按之后进行拖动,PanGesture手势被触发 PanGesture() .onActionStart(() => { this.borderStyles = BorderStyle.Dashed; console.info('pan start'); }) // 当该手势被触发时,根据回调获得拖动的距离,修改该组件的位移距离从而实现组件的移动 .onActionUpdate((event: GestureEvent) => { this.offsetX = this.positionX + event.offsetX; this.offsetY = this.positionY + event.offsetY; console.info('pan update'); }) .onActionEnd(() => { this.positionX = this.offsetX; this.positionY = this.offsetY; this.borderStyles = BorderStyle.Solid; }) ) ) }}
// xxx.ets
@Entry
@Component
struct Index {
@State offsetX: number = 0;
@State offsetY: number = 0;
@State count: number = 0;
@State positionX: number = 0;
@State positionY: number = 0;
@State borderStyles: BorderStyle = BorderStyle.Solid
build() {
Column() {
Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
.fontSize(28)
}
// 绑定translate属性可以实现组件的位置移动
.translate({ x: this.offsetX, y: this.offsetY, z: 0 })
.height(250)
.width(300)
//以下组合手势为顺序识别,当长按手势事件未正常触发时不会触发拖动手势事件
.gesture(
// 声明该组合手势的类型为Sequence类型
GestureGroup(GestureMode.Sequence,
// 该组合手势第一个触发的手势为长按手势,且长按手势可多次响应
LongPressGesture({ repeat: true })
// 当长按手势识别成功,增加Text组件上显示的count次数
.onAction((event: GestureEvent) => {
if (event.repeat) {
this.count++;
console.info('LongPress onAction');
})
.onActionEnd(() => {
console.info('LongPress end');
}),
// 当长按之后进行拖动,PanGesture手势被触发
PanGesture()
.onActionStart(() => {
this.borderStyles = BorderStyle.Dashed;
console.info('pan start');
// 当该手势被触发时,根据回调获得拖动的距离,修改该组件的位移距离从而实现组件的移动
.onActionUpdate((event: GestureEvent) => {
this.offsetX = this.positionX + event.offsetX;
this.offsetY = this.positionY + event.offsetY;
console.info('pan update');
this.positionX = this.offsetX;
this.positionY = this.offsetY;
this.borderStyles = BorderStyle.Solid;
)
组合手势的并行识别是指同时识别多个手势的能力。通常,组合手势是由多个基本手势组合而成的,例如在手机屏幕上的滑动、横扫或双击等。并行识别允许系统同时检测和识别多个手势,而不是一次只能识别一个手势。这样可以提高交互效率和用户体验,使用户能够更自由和灵活地操作设备。
// xxx.ets@Entry@Componentstruct Index { @State count1: number = 0; @State count2: number = 0; build() { Column() { Text('parallel gesture\n' + 'tapGesture count is 1:' + this.count1 + '\ntapGesture count is 2:' + this.count2 + '\n') .fontSize(28) } .height(200) .width(250) // 以下组合手势为并行并别,单击手势识别成功后,若在规定时间内再次点击,双击手势也会识别成功 .gesture( GestureGroup(GestureMode.Parallel, TapGesture({ count: 1 }) .onAction(() => { this.count1++; }), TapGesture({ count: 2 }) .onAction(() => { this.count2++; }) ) ) }}
@State count1: number = 0;
@State count2: number = 0;
Text('parallel gesture\n' + 'tapGesture count is 1:' + this.count1 + '\ntapGesture count is 2:' + this.count2 + '\n')
.height(200)
.width(250)
// 以下组合手势为并行并别,单击手势识别成功后,若在规定时间内再次点击,双击手势也会识别成功
GestureGroup(GestureMode.Parallel,
TapGesture({ count: 1 })
.onAction(() => {
this.count1++;
TapGesture({ count: 2 })
this.count2++;
组合手势互斥识别是指在多种手势操作同时发生时,系统通过识别这些手势的组合,来判断用户的意图并执行相应的操作。互斥识别是指系统能够判断两种或多种手势的组合是否冲突,如果冲突则只执行其中一种手势的操作,避免产生意料之外的结果或混乱。例如,在触摸屏设备上,用户同时进行滑动和放大手势操作时,系统可以通过互斥识别来判断用户是要进行滑动操作还是放大操作,并执行相应的操作。通过组合手势互斥识别,可以提高用户界面的交互性和操作的准确性。
// xxx.ets@Entry@Componentstruct Index { @State count1: number = 0; @State count2: number = 0; build() { Column() { Text('parallel gesture\n' + 'tapGesture count is 1:' + this.count1 + '\ntapGesture count is 2:' + this.count2 + '\n') .fontSize(28) } .height(200) .width(250) //以下组合手势为互斥并别,单击手势识别成功后,双击手势会识别失败 .gesture( GestureGroup(GestureMode.Exclusive, TapGesture({ count: 1 }) .onAction(() => { this.count1++; }), TapGesture({ count: 2 }) .onAction(() => { this.count2++; }) ) ) }}
//以下组合手势为互斥并别,单击手势识别成功后,双击手势会识别失败
GestureGroup(GestureMode.Exclusive,
原文链接:https://www.cnblogs.com/shudaoshan/p/18188295
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728