Enhance ScannerView logging for code selection and add debug overlays; implement touch feedback for code markers and improve touch area responsiveness.

main
v504 2 months ago
parent 11ad633b1d
commit 65fb011089

@ -64,8 +64,8 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "167"
endingLineNumber = "167"
landmarkName = "body"
landmarkType = "24">
landmarkName = "ScanningInstructionView"
landmarkType = "14">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>

@ -87,9 +87,15 @@ struct ScannerView: View {
}
private func handleCodeSelection(_ selectedCode: DetectedCode) {
logInfo("用户选择了条码: \(selectedCode.content)", className: "ScannerView")
logInfo("🎯 ScannerView 收到条码选择回调", className: "ScannerView")
logInfo(" 选择的条码ID: \(selectedCode.id)", className: "ScannerView")
logInfo(" 选择的条码类型: \(selectedCode.type)", className: "ScannerView")
logInfo(" 选择的条码内容: \(selectedCode.content)", className: "ScannerView")
logInfo(" 选择的条码位置: \(selectedCode.bounds)", className: "ScannerView")
let formattedResult = "类型: \(selectedCode.type)\n内容: \(selectedCode.content)"
logInfo(" 格式化结果: \(formattedResult)", className: "ScannerView")
NotificationCenter.default.post(name: .scannerDidScanCode, object: formattedResult)
dismiss()
}
@ -435,9 +441,41 @@ struct CodePositionOverlay: View {
onCodeSelected: onCodeSelected
)
}
//
#if DEBUG
ForEach(detectedCodes) { code in
let position = calculateDebugPosition(code: code, screenSize: geometry.size)
Rectangle()
.stroke(Color.red, lineWidth: 1)
.frame(width: 80, height: 80)
.position(x: position.x, y: position.y)
.opacity(0.3)
}
#endif
}
}
.allowsHitTesting(true)
.contentShape(Rectangle()) //
.zIndex(1000) //
}
//
private func calculateDebugPosition(code: DetectedCode, screenSize: CGSize) -> CGPoint {
guard let previewLayer = previewLayer else {
return CGPoint(x: screenSize.width / 2, y: screenSize.height / 2)
}
let metadataObject = code.bounds
let convertedPoint = previewLayer.layerPointConverted(fromCaptureDevicePoint: CGPoint(
x: metadataObject.midX,
y: metadataObject.midY
))
let clampedX = max(40, min(screenSize.width - 40, convertedPoint.x))
let clampedY = max(40, min(screenSize.height - 40, convertedPoint.y))
return CGPoint(x: clampedX, y: clampedY)
}
}
@ -453,6 +491,25 @@ struct CodePositionMarker: View {
let position = calculatePosition(screenSize: geometry.size)
ZStack {
//
Circle()
.fill(Color.clear)
.frame(width: 80, height: 80)
.contentShape(Circle())
.onTapGesture {
logDebug("🎯 CodePositionMarker 被点击!", className: "CodePositionMarker")
logDebug(" 条码ID: \(code.id)", className: "CodePositionMarker")
logDebug(" 条码类型: \(code.type)", className: "CodePositionMarker")
logDebug(" 条码内容: \(code.content)", className: "CodePositionMarker")
logDebug(" 点击位置: x=\(position.x), y=\(position.y)", className: "CodePositionMarker")
//
let impactFeedback = UIImpactFeedbackGenerator(style: .medium)
impactFeedback.impactOccurred()
onCodeSelected(code)
}
//
Circle()
.stroke(Color.green, lineWidth: 3)
@ -469,14 +526,7 @@ struct CodePositionMarker: View {
.frame(width: 6, height: 6)
}
.position(x: position.x, y: position.y)
.background(
Circle()
.fill(Color.clear)
.frame(width: 60, height: 60)
)
.onTapGesture {
onCodeSelected(code)
}
.zIndex(1001) //
.onAppear {
logDebug("CodePositionMarker appeared at: x=\(position.x), y=\(position.y)", className: "CodePositionMarker")
logDebug("Screen size: \(geometry.size)", className: "CodePositionMarker")

@ -0,0 +1,194 @@
# 触摸选择点响应问题修复说明
## 🚨 问题描述
用户反馈存在点击选择点没有响应的问题,即点击二维码位置标记时无法选择对应的二维码。
## 🔍 问题分析
经过代码分析,发现以下几个可能导致触摸无响应的问题:
### 1. **触摸区域定义问题**
- 触摸手势绑定在 ZStack 上,但触摸区域可能不够明确
- 触摸区域的大小和位置可能不正确
### 2. **层级问题**
- 触摸事件可能被其他视图拦截
- 缺少明确的 zIndex 设置
### 3. **触摸事件传播问题**
- GeometryReader 可能影响触摸事件的传播
- 触摸区域的 contentShape 设置不正确
### 4. **触摸区域大小问题**
- 触摸区域太小,用户难以准确点击
- 触摸区域与视觉标记不匹配
## 🛠️ 修复方案
### 1. **优化触摸区域定义**
```swift
// 修复前:触摸区域定义不明确
.background(
Circle()
.fill(Color.clear)
.frame(width: 60, height: 60)
)
.onTapGesture {
onCodeSelected(code)
}
// 修复后:明确的触摸区域定义
ZStack {
// 触摸区域背景(透明,但可以接收触摸事件)
Circle()
.fill(Color.clear)
.frame(width: 80, height: 80) // 增大触摸区域
.contentShape(Circle()) // 明确触摸形状
.onTapGesture {
// 触摸处理逻辑
}
// 视觉标记
// ...
}
```
### 2. **添加层级管理**
```swift
// 在 CodePositionOverlay 中添加
.zIndex(1000) // 确保在最上层,不被其他视图遮挡
// 在 CodePositionMarker 中添加
.zIndex(1001) // 确保触摸区域在最上层
```
### 3. **增强触摸事件处理**
```swift
.onTapGesture {
logDebug("🎯 CodePositionMarker 被点击!", className: "CodePositionMarker")
logDebug(" 条码ID: \(code.id)", className: "CodePositionMarker")
logDebug(" 条码类型: \(code.type)", className: "CodePositionMarker")
logDebug(" 条码内容: \(code.content)", className: "CodePositionMarker")
logDebug(" 点击位置: x=\(position.x), y=\(position.y)", className: "CodePositionMarker")
// 添加触觉反馈
let impactFeedback = UIImpactFeedbackGenerator(style: .medium)
impactFeedback.impactOccurred()
onCodeSelected(code)
}
```
### 4. **添加调试功能**
```swift
// 在 DEBUG 模式下显示触摸区域边界
#if DEBUG
ForEach(detectedCodes) { code in
let position = calculateDebugPosition(code: code, screenSize: geometry.size)
Rectangle()
.stroke(Color.red, lineWidth: 1)
.frame(width: 80, height: 80)
.position(x: position.x, y: position.y)
.opacity(0.3)
}
#endif
```
## 📱 修复后的触摸体验
### 1. **触摸区域**
- 触摸区域从 60x60 增加到 80x80 像素
- 触摸区域与视觉标记中心对齐
- 触摸区域使用 `contentShape(Circle())` 确保圆形触摸
### 2. **视觉反馈**
- 添加触觉反馈,用户点击时会有震动
- 调试模式下显示触摸区域边界(红色矩形)
- 触摸区域足够大,易于点击
### 3. **层级管理**
- `CodePositionOverlay`: zIndex 1000
- `CodePositionMarker`: zIndex 1001
- 确保触摸区域在最上层,不被遮挡
### 4. **调试信息**
- 详细的触摸事件日志
- 触摸位置和条码信息记录
- 触摸区域可视化DEBUG 模式)
## 🧪 测试方法
### 1. **基本触摸测试**
1. 扫描包含多个二维码的图片
2. 等待预览暂停状态
3. 点击不同的绿色标记
4. 检查控制台日志输出
5. 验证选择结果是否正确
### 2. **触摸区域测试**
1. 在 DEBUG 模式下运行
2. 观察红色矩形边界(触摸区域)
3. 点击边界内不同位置
4. 验证触摸响应是否一致
### 3. **边界情况测试**
1. 点击触摸区域边缘
2. 快速连续点击
3. 在不同屏幕尺寸下测试
4. 在不同设备方向上测试
## 🔧 进一步优化建议
### 1. **触摸区域优化**
- 根据设备类型动态调整触摸区域大小
- 添加触摸区域的热点指示器
- 优化触摸区域的形状(可能使用更复杂的形状)
### 2. **用户体验改进**
- 添加触摸动画效果
- 实现触摸区域的视觉反馈
- 添加触摸音效
### 3. **性能优化**
- 优化触摸事件的处理逻辑
- 减少不必要的视图更新
- 优化触摸区域的渲染
### 4. **可访问性改进**
- 支持 VoiceOver 导航
- 添加触摸区域的辅助功能标签
- 支持更大的触摸目标
## 📋 修复检查清单
- ✅ 触摸区域大小从 60x60 增加到 80x80
- ✅ 添加明确的 `contentShape(Circle())`
- ✅ 设置正确的 zIndex 层级
- ✅ 添加触觉反馈
- ✅ 增强触摸事件日志
- ✅ 添加调试模式触摸区域可视化
- ✅ 优化触摸事件处理逻辑
- ✅ 确保触摸区域与视觉标记对齐
## 🎯 预期效果
修复后,用户应该能够:
1. **准确触摸**:触摸区域足够大,易于点击
2. **即时反馈**:点击时有触觉反馈和日志输出
3. **正确选择**:触摸事件正确传递到选择逻辑
4. **调试友好**:可以通过日志和可视化工具诊断问题
## 🚀 部署说明
1. **编译验证**:确保项目能够正常编译
2. **功能测试**:测试触摸选择功能是否正常
3. **性能测试**:验证触摸响应是否流畅
4. **用户测试**:让实际用户测试触摸体验
通过这些修复,触摸选择点无响应的问题应该得到根本解决,用户体验将显著提升。
Loading…
Cancel
Save