菜鸟教程小白 发表于 2022-12-11 19:17:09

ios - Swift 3 将长按手势添加到 Mapbox 注释


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

<pre><code>func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
}
</code></pre>

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

<p>提前感谢您的回答</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我不熟悉 Mapbox API,但如果没有合适的委托(delegate)方法,请尝试使用 uigesturerecognizer 和委托(delegate)自定义实现。</p>

<p>在注释 View 上设置您的手势识别器:</p>

<pre><code>let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressDetected))
annotation.view.addGestureRecognizer(longPressGestureRecognizer)
</code></pre>

<p>做一个代表</p>

<pre><code>weak var delegate: AnnotationViewDelegate?
</code></pre>

<p>和Annotation子类中的协议(protocol)<code>AnnotationViewDelegate</code>:</p>

<pre><code>protocol AnnotationViewDelegate: class {
    func annotationDidDetectLongPress()
}
</code></pre>

<p>实现长按处理程序并通知代理内部长按</p>

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

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

<pre><code>func annotationDidDetectLongPress() {
    // done
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift 3 将长按手势添加到 Mapbox 注释,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42846867/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42846867/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift 3 将长按手势添加到 Mapbox 注释