2.事件

通用事件

通用事件API

组件区域变化事件文档中心

组件区域变化时触发

  • 返回目标元素变化之前的宽高以及目标元素相对父元素和页面左上角的坐标位置
  • 返回目标元素变化之后的宽高以及目标元素相对父元素和页面左上角的坐标位置
.onAreaChange((_oldValue, _newValue) => {
// onAreaChange可能会触发多次,所以不要批量使用
promptAction.showDialog({
message: `
宽度:${_oldValue.width} - ${_newValue.width}
高度:${_oldValue.height} - ${_newValue.height}
X坐标:${_oldValue.globalPosition.x} - ${_newValue.globalPosition.x}
Y坐标:${_oldValue.globalPosition.y} - ${_newValue.globalPosition.y}
`
})

手势事件文档中心

单一手势

长按手势

// fingers: 1,触发长按手势所需要的最少手指数量
// repeat: true,否连续触发事件回调
// priority: 500,触发长按所需的最短时间,单位为毫秒,默认值为500
.gesture(LongPressGesture({ repeat: true }).onAction((event: GestureEvent | undefined) => {
if (event) {
if (event.repeat) {
console.log('长按', item.deviceViewId.toString());
}
}
}))

组合手势

互斥识别

.gesture(
GestureGroup(GestureMode.Exclusive,
TapGesture({ count: 1 })
.onAction(() => {
console.log('单击');
}),
LongPressGesture()
.onAction(() => {
console.log('长按');
})
)
)