Swift - 如何要求 UIGestureRecognizer 函数返回值

标签 swift mkmapview uigesturerecognizer

我想要求函数注释返回坐标(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/

相关文章:

json - 在 Swift Playground 中通过 POST 发送图像

ios - 如何在 View Controller 之间正确进行分隔

ios - mapkit xcode 8 中的 onclick 事件弹出窗口

swift - 使用 Swift 进行正向地理编码

ios - 苹果 map 上的方向

ios - 从平移手势中剪切对角线移动

swift - 核心数据 - 改变属性类型

ios - 如何只添加一次 view.addGestureRecognizer?

objective-c - 为什么 UITableviewcell 不完整的滑动行充当完整的滑动行?

ios - NSPredicate 格式动态使用属性名称