首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >LongPress in MapKit

LongPress in MapKit
EN

Stack Overflow用户
提问于 2016-11-07 14:50:07
回答 2查看 496关注 0票数 2

我是一个在Swift 3中的应用程序。

所有的接缝都正常工作,但是,当我在地图上longPress时,它的作用就像长按两次。我不知道为什么。我在印刷机里面做了一个打印,每次我按下(一次)它会检测到两个长的印刷机.为什么会发生这种事?这有什么问题吗?

代码语言:javascript
运行
复制
import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet var map: MKMapView!

var numberOfLongPress : Int = 0

override func viewDidLoad() {
    super.viewDidLoad()


    let latitude: CLLocationDegrees = 38.925560

    let longitude: CLLocationDegrees = -9.229723

    let lanDelta: CLLocationDegrees = 0.05

    let lonDelta: CLLocationDegrees = 0.05

    let span = MKCoordinateSpan(latitudeDelta: lanDelta, longitudeDelta: lonDelta)

    let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    let region = MKCoordinateRegion(center: coordinates, span: span)

    map.setRegion(region, animated: true)

    let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:)))

    lpgr.minimumPressDuration = 0.5

    map.addGestureRecognizer(lpgr)

}

func longpress(gestureRecognizer: UIGestureRecognizer) {

    let touchPoint = gestureRecognizer.location(in: self.map)

    let coordinate = map.convert(touchPoint, toCoordinateFrom: self.map)

    let annotation = MKPointAnnotation()

    annotation.coordinate = coordinate

    annotation.title = "My Place"



    map.addAnnotation(annotation)

    print("longpress activated")

    numberOfLongPress = numberOfLongPress + 1

    print(numberOfLongPress) //detect number of long presses

}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-07 15:01:53

在每次状态更改时都会调用选择器,所以您最好检查状态并在.began.ended上执行您需要做的事情。

代码语言:javascript
运行
复制
func longpress(gestureRecognizer: UIGestureRecognizer) {
    guard gestureRecognizer.state == .began else { return }

    // add annotation
}
票数 3
EN

Stack Overflow用户

发布于 2016-11-07 15:05:08

让我们打印gestureRecognizer的状态,您将看到.began.ended。因此,在添加注释之前,让我们检查一下gestureRecognizer的状态。

代码语言:javascript
运行
复制
func longpress(gestureRecognizer: UILongPressGestureRecognizer) {
    if gestureRecognizer.state == began {
        // do something here
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40468029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档