OGeek|极客世界-中国程序员成长平台

标题: ios - Swift 3 将长按手势添加到 Mapbox 注释 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:17
标题: ios - Swift 3 将长按手势添加到 Mapbox 注释

我正在尝试弄清楚如何将长按手势添加到 map 注释(Mapbox)。我设置了我的代码,以便当用户点击注释时,他们通过将我的代码放入其中来切换到另一个 View 功能。

func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
}

现在我想允许用户通过持有相同的注释来切换到另一个 View 。我尝试在上面的代码中使用 if 和 else 语句,但长按手势不起作用,除非我先点击注释以激活该功能,以便 if 和 else 语句可以开始工作。但我不希望用户点击然后按住。我只想让他们点击或按住注释。

提前感谢您的回答



Best Answer-推荐答案


我不熟悉 Mapbox API,但如果没有合适的委托(delegate)方法,请尝试使用 uigesturerecognizer 和委托(delegate)自定义实现。

在注释 View 上设置您的手势识别器:

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressDetected))
annotation.view.addGestureRecognizer(longPressGestureRecognizer)

做一个代表

weak var delegate: AnnotationViewDelegate?

和Annotation子类中的协议(protocol)AnnotationViewDelegate:

protocol AnnotationViewDelegate: class {
    func annotationDidDetectLongPress()
}

实现长按处理程序并通知代理内部长按

func longPressDetected(sender: UILongPressGestureRecognizer) {
    // here you should notify the delegate 
    delegate?.annotationDidDetectLongPress()
}

在 Controller 中将委托(delegate)分配给 self 并实现

func annotationDidDetectLongPress() {
    // done
}

关于ios - Swift 3 将长按手势添加到 Mapbox 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42846867/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4