gpt4 book ai didi

ios - 带有 MKMapKit 的 Swift GestureRecognizers - 放下图钉并拖动

转载 作者:搜寻专家 更新时间:2023-10-30 22:00:29 26 4
gpt4 key购买 nike

我正在尝试将多个 UIGestureReconizers 与一个 MKMapView 结合使用,用户可以将图钉放在上面并四处拖动。它有一个 callout。我在 TabbarControllerNavigationController 中实现它。我目前有:

1) PanGestureRecognizer 将 Tabbar 和 Navigation 项设置为动画离开屏幕。这在不干扰平移 map 的情况下工作正常。

2) 设置为一次点击的 TapGestureRecognizer 使两个项目从 1) 动画回到屏幕上。

3) TapGestureRecognizer 设置为两次点击允许底层 MKMapView 缩放功能起作用。此 GestureRecognizer 的 delegate 具有 gestureRecognizer。shouldRecognizeSimultaneouslyWithGestureRecognizer 设置为 true

这些在 viewDidLoad 中设置如下:

 // This sets up the pan gesture recognizer to hide the bars from the UI.
let panRec: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:")
panRec.delegate = self
mapView.addGestureRecognizer(panRec)

// This sets up the tap gesture recognizer to un-hide the bars from the UI.
let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didTapMap:")
singleTap.delegate = self
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
mapView.addGestureRecognizer(singleTap)

// This sets up the double tap gesture recognizer to enable the zoom facility.
// In order to pass double-taps to the underlying `MKMapView` the delegate for this recognizer (self) needs to return true from
// gestureRecognizer.shouldRecognizeSimultaneouslyWithGestureRecognizer
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer()
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
doubleTap.delegate = self
mapView.addGestureRecognizer(doubleTap)

// This delays the single-tap recognizer slightly and ensures that it will NOT fire if there is a double-tap
singleTap.requireGestureRecognizerToFail(doubleTap)

当我尝试实现 UILongPressGestureRecognizer 以允许将图钉放到 map 上时,我的问题就出现了。我正在尝试使用添加到 viewDidLoad 的以下内容:

// This sets up the long tap to drop the pin.
let longTap: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "didLongTapMap:")
longTap.delegate = self
longTap.numberOfTapsRequired = 0
longTap.minimumPressDuration = 0.5
mapView.addGestureRecognizer(longTap)

这是我的操作方法:

func didLongTapMap(gestureRecognizer: UIGestureRecognizer) {
// Get the spot that was tapped.
let tapPoint: CGPoint = gestureRecognizer.locationInView(mapView)
let touchMapCoordinate: CLLocationCoordinate2D = mapView.convertPoint(tapPoint, toCoordinateFromView: mapView)

var viewAtBottomOfHierarchy: UIView = mapView.hitTest(tapPoint, withEvent: nil)
if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView {
return
} else {
if .Began == gestureRecognizer.state {
// Delete any existing annotations.
if mapView.annotations.count != 0 {
mapView.removeAnnotations(mapView.annotations)
}

annotation = MKPointAnnotation()
annotation.coordinate = touchMapCoordinate

mapView.addAnnotation(annotation)
_isPinOnMap = true

findAddressFromCoordinate(annotation.coordinate)
updateLabels()
}
}
}

这确实允许在长按时放下图钉,单击一次将显示标注,但如果拖动启动不够快,则第二次点击以按住并拖动会导致第二个图钉掉落。第二个 pin 掉落到前一个 pin 悬停的空间中,用户可以拖动,但新的 pin 掉落很尴尬且看起来不对。

我正在尝试使用这条线:

if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView {

将水龙头返回到 MKMapView 并防止另一个 pin 被删除,但返回永远不会被调用,即使此行上的断点显示 viewAtBottom 的类型为 MapKit.MKPinAnnotationView。我哪里出错了?

最佳答案

如果我理解正确的话,我想我可能会找到你问题的答案。当放下一个图钉然后在不放置另一个图钉的情况下四处拖动屏幕时遇到问题,对吗?这是我的代码,我一直在做类似的事情,这似乎对我有用。导入 UIKit导入 MapKit导入核心位置

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet var map: MKMapView!

var manager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


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

uilpgr.minimumPressDuration = 2

map.addGestureRecognizer(uilpgr)

if activePlace == -1 {

manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
self.map.showsUserLocation = true

} else {

//GET PLACE DETAILS TO DISPLAY ON MAP

if places.count > activePlace {

if let name = places[activePlace]["name"]{

if let lat = places[activePlace]["lat"]{

if let lon = places[activePlace]["lon"]{

if let latitude = Double(lat) {

if let longitude = Double(lon) {

let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)

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

let region = MKCoordinateRegionMake(coordinate, span)

self.map.setRegion(region, animated: true)

let annotation = MKPointAnnotation()

annotation.coordinate = coordinate

annotation.title = name

self.map.addAnnotation(annotation)

}

}

}

}

}

}

}


}


func longpress(gestureRecognizer: UIGestureRecognizer) {

if gestureRecognizer.state == UIGestureRecognizerState.began {

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

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

let location = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)

var title = ""

CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in

if error != nil {

print(error)

} else {

if let placemark = placemarks?[0] {

if placemark.subThoroughfare != nil {

title += placemark.subThoroughfare! + " "

}

if placemark.thoroughfare != nil {

title += placemark.thoroughfare! + " "

}



}

}

if title == "" {

title = "Added \(NSDate())"

}

let annotation = MKPointAnnotation()

annotation.coordinate = newCoordinate

annotation.title = title

self.map.addAnnotation(annotation)

places.append(["name": title, "lat":String(newCoordinate.latitude), "lon":String(newCoordinate.longitude)])

UserDefaults.standard.set(places, forKey: "places")

})

}

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)

let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)

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

self.map.setRegion(region, animated: true)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

希望对您有所帮助,问题也可能是您的最短长按持续时间仅为 0.5。

关于ios - 带有 MKMapKit 的 Swift GestureRecognizers - 放下图钉并拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24777919/

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