getPinch method

M3PinchInfo? getPinch()

🔥 核心:取得 pinch(兩指捏合)資訊

Implementation

M3PinchInfo? getPinch() {
  if (touches.length != 2) {
    _prevPinchDistance = null;
    return null;
  }
  final keys = touches.keys.toList();
  final t1 = touches[keys[0]]!;
  final t2 = touches[keys[1]]!;

  final p1 = t1.path.last.position;
  final p2 = t2.path.last.position;

  // 當前距離
  final distance = (p2 - p1).length;

  // 中心點
  final center = Vector2((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);

  // 如果之前沒有資料 → 初始化,不回傳 pinch(避免跳動)
  if (_prevPinchDistance == null) {
    _prevPinchDistance = distance;
    return null;
  }

  // 計算縮放比
  final scale = distance / _prevPinchDistance!;

  // 更新上一次距離
  _prevPinchDistance = distance;

  return M3PinchInfo(scale, center, distance);
}