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)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 本来是内部做分享交流Core Text,懒得做PPT,坐标系转换这块单独写篇文章倒省事😆 正文 上篇文章从零...
- 前言 本来不想做笔记记录,后来想想坐标系转换这块单独还是要写一下,方便初步理解YYText整体构建过程。 正文 C...