Reason:TypeError
Error name:TypeError
Error message:Cannot read property localX of undefined
Stacktrace:
Cannot get SourceMap info, dump raw stack:
at anonymous (entry|entry|1.0.0|src/main/ets/components/CGBGestureUnlockView.ts:235:1) 在使用前判断FingerInfo对象是否为空。
@Entry
@Component
struct PanGesturePage {
@State offsetX: number = 0;
@State offsetY: number = 0;
private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right });
build() {
Column() {
Column() {
Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
}
.height(200)
.width(300)
.padding(20)
.border({ width: 3 })
.margin(50)
.gesture(
PanGesture(this.panOption)
.onActionStart((event: GestureEvent) => {
// 获取fingerInfos信息
if (event?.fingerList) {
// 使用前判断FingerInfo对象是否为空
if (event.fingerList[0] !== undefined) {
this.offsetX = event.fingerList[0].localX;
this.offsetY = event.fingerList[0].localY;
}
}
})
)
}
}
} 上述为常规修复方案,在API20及以上版本,GestureEvent对象新增属性fingerInfos,该属性返回的是当前触摸手势的所有触摸点,不存在空值情况。