我想要求函数注释返回坐标(CLLocationCoordinate2D)以供其他函数使用,这是我的部分代码:
// ULMap is MapView.
override func viewDidLoad() {
var longPressGR = UILongPressGestureRecognizer(target: self, action: "annotation:")
longPressGR.minimumPressDuration = 1
UImap.addGestureRecognizer(longPressGR)
}
func annotation(gesture: UIGestureRecognizer){
//Coordinate
var touchPoint = gesture.locationInView(self.UImap)
var coordinate = UImap.convertPoint(touchPoint, toCoordinateFromView: self.UImap)
}
我试过这个,但它不起作用:
func annotation(gesture: UIGestureRecognizer) -> CLLocationCoordinate2D{
//Coordinate
var touchPoint = gesture.locationInView(self.UImap)
var coordinate = UImap.convertPoint(touchPoint, toCoordinateFromView: self.UImap)
return coordinate
}
有没有办法做到这一点?提前致谢。
最佳答案
像手势识别器调用之类的东西不能返回任何东西,因为你不是调用它们的人。它们被系统调用,因此任何返回值都将通过您无权访问的代码向上传播。您应该为您的坐标创建一个类级变量并进行设置。
所以与其说 var coordinate = UImap.convertPoint(touchPoint, toCoordinateFromView: self.UImap)
你声明
var coordinate:CLLocationCoordinate2D
在类范围内,然后在你的函数中
coordinate = UImap.convertPoint(touchPoint, toCoordinateFromView: self.UImap)
那么坐标将永远是最近设置的坐标。如果需要跟踪多个,可以将它们添加到一个数组中。
关于Swift - 如何要求 UIGestureRecognizer 函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250235/