我是一个在Swift 3中的应用程序。
所有的接缝都正常工作,但是,当我在地图上longPress时,它的作用就像长按两次。我不知道为什么。我在印刷机里面做了一个打印,每次我按下(一次)它会检测到两个长的印刷机.为什么会发生这种事?这有什么问题吗?
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
}}
发布于 2016-11-07 15:01:53
在每次状态更改时都会调用选择器,所以您最好检查状态并在.began或.ended上执行您需要做的事情。
func longpress(gestureRecognizer: UIGestureRecognizer) {
guard gestureRecognizer.state == .began else { return }
// add annotation
}发布于 2016-11-07 15:05:08
让我们打印gestureRecognizer的状态,您将看到.began和.ended。因此,在添加注释之前,让我们检查一下gestureRecognizer的状态。
func longpress(gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == began {
// do something here
}
}https://stackoverflow.com/questions/40468029
复制相似问题