美文网首页
iOS开发触摸点转换坐标系(swift)

iOS开发触摸点转换坐标系(swift)

作者: Flynn_Lee | 来源:发表于2023-12-14 11:52 被阅读0次
import UIKit

class MapController: UIViewController {

        lazy var mapView: MapView = {
        let mapV = MapView()
        mapV.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - kNavigationBarHeight() - 44)
        return mapV
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(mapView)
        // 添加Pan手势识别器到需要监听手指移动的视图上
        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
        mapView.addGestureRecognizer(panGestureRecognizer)
    }
    
    @objc func handlePanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
        
        switch gestureRecognizer.state {
        case .began:

            break
        case .changed:
            //获取手指在地图view上触摸点的x和y
            let locationInView = gestureRecognizer.location(in: mapView)
            //将地图上x和y转换到自身view的坐标系
            let touchPoint = mapView.convert(locationInView, to: self.coordinateSpace)
            /**
            这个地方我需要做一个触摸跟随的动画,必须让触摸点的图片紧紧跟随在我手指下,所以必须转成精准的坐标系
            */
            // 在这里你可以处理手指移动的点            
            let touchCoord = mapView.convert(touchPoint, toCoordinateFrom: mapView)
            print("手指活动:\(touchCoord.latitude)---\(touchCoord.longitude)")
  
            break
        case .ended:
            // 重置translation,以便下一次移动计算
            gestureRecognizer.setTranslation(.zero, in: mapView)
            break
            
        default:
            break
        }
    }
}

相关文章

网友评论

      本文标题:iOS开发触摸点转换坐标系(swift)

      本文链接:https://www.haomeiwen.com/subject/cwoigdtx.html