QSLHomeAnnotatinView.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // QSLHomeAnnotatinView.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/6.
  6. //
  7. import UIKit
  8. import MAMapKit
  9. class QSLHomeAnnotatinView: MAAnnotationView {
  10. var title: String?
  11. var calloutView: MapLocationCalloutView?
  12. override func setSelected(_ selected: Bool, animated: Bool) {
  13. if self.isSelected == selected {
  14. return
  15. }
  16. if selected {
  17. if calloutView == nil {
  18. let width = (title?.widthAccording(height: 20.rpx, font: UIFont.textM(14), lineSpacing: 0) ?? 0)
  19. calloutView = MapLocationCalloutView(frame: CGRect(x: 0, y: 0, width: width + 32.rpx, height: 20.rpx))
  20. if let calloutView = calloutView {
  21. calloutView.center = CGPoint(x: bounds.size.width / 2 + calloutOffset.x,
  22. y: -calloutView.bounds.size.height / 2 + calloutOffset.y)
  23. }
  24. }
  25. calloutView?.name = title
  26. if let calloutView = calloutView {
  27. addSubview(calloutView)
  28. }
  29. } else {
  30. calloutView?.removeFromSuperview()
  31. }
  32. super.setSelected(selected, animated: animated)
  33. }
  34. override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
  35. var view = super.hitTest(point, with: event)
  36. if view == nil {
  37. let convertedPoint = convert(point, from: self)
  38. if bounds.contains(convertedPoint) {
  39. view = self
  40. }
  41. }
  42. return view
  43. }
  44. }