gpt4 book ai didi

swift - MapKit 中的长按

转载 作者:可可西里 更新时间:2023-11-01 00:36:05 26 4
gpt4 key购买 nike

我正在使用 Swift 3 构建应用。

一切似乎都工作正常,但是当我在 map 中长按时,它就像被长按两次一样。我不知道为什么...我在长压机内做了一个打印,以计算长压机,每次我长压机(一次)它都会检测到两次长压机……为什么会这样?有什么问题吗?

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

}

最佳答案

每次状态更改时都会调用选择器,因此您最好检查状态并在 .began.ended 上执行您需要执行的操作。

func longpress(gestureRecognizer: UIGestureRecognizer) {
guard gestureRecognizer.state == .began else { return }

// add annotation
}

关于swift - MapKit 中的长按,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com