添加到 MKMapView 时的 UILongPressGestureRecognizer 问题
Posted
技术标签:
【中文标题】添加到 MKMapView 时的 UILongPressGestureRecognizer 问题【英文标题】:UILongPressGestureRecognizer issue when adding to MKMapView 【发布时间】:2015-12-17 05:39:52 【问题描述】:我还是 swift 新手,正在尝试弄清楚如何在地图视图上添加长按手势注释。
但是,我一直收到此错误:
libc++abi.dylib:以未捕获的类型异常终止 NSException
我的 addAnotation 函数有问题吗?
提前致谢。
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
// action = A selector that identifies the method implemented by the target to handle the gesture recognized by the receiver. The action selector must conform to the signature described in the class overview. NULL is not a valid value.
var uilpgr = UILongPressGestureRecognizer(target: self, action: "addAnotation")
uilpgr.minimumPressDuration = 2.0
map.addGestureRecognizer(uilpgr)
func addAnotation(gestureRecognizer:UIGestureRecognizer)
if(gestureRecognizer.state == UIGestureRecognizerState.Began)
//locationInView = Returns the point computed as the location in a given view of the gesture represented by the receiver.
var touchPoint = gestureRecognizer.locationInView(self.map)
//convertPoint = convert a point from map to coordinate
var newCoordinate = self.map.convertPoint(touchPoint, toCoordinateFromView: self.map)
var annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = "New Annotation"
self.map.addAnnotation(annotation)
以下是完整代码供您参考:
import UIKit
import MapKit
class ViewController: UIViewController, 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.
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
// action = A selector that identifies the method implemented by the target to handle the gesture recognized by the receiver. The action selector must conform to the signature described in the class overview. NULL is not a valid value.
var uilpgr = UILongPressGestureRecognizer(target: self, action: "addAnotation")
uilpgr.minimumPressDuration = 2.0
map.addGestureRecognizer(uilpgr)
func addAnotation(gestureRecognizer:UIGestureRecognizer)
if(gestureRecognizer.state == UIGestureRecognizerState.Began)
//locationInView = Returns the point computed as the location in a given view of the gesture represented by the receiver.
var touchPoint = gestureRecognizer.locationInView(self.map)
//convertPoint = convert a point from map to coordinate
var newCoordinate = self.map.convertPoint(touchPoint, toCoordinateFromView: self.map)
var annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = "New Annotation"
self.map.addAnnotation(annotation)
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
print(locations)
var userLocation:CLLocation = locations[0]
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var coordinate = CLLocationCoordinate2DMake(latitude, longitude)
var latDelta:CLLocationDegrees = 0.01
var lonDelta:CLLocationDegrees = 0.01
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta) // MKCoordinateSpan = A structure that defines the area spanned by a map region.
// Mk CoordinateRegion = A structure that defines which portion of the map to display.
var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
self.map.setRegion(region, animated: true)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
【参考方案1】:你只需要在addAnotation
后面加一个冒号:
var uilpgr = UILongPressGestureRecognizer(target: self, action: "addAnotation:")
在您省略冒号的版本中,将调用具有此签名的方法。注意没有参数
func addAnotation()
所以您的 UILongPressGestureRecognizer
正在尝试调用上述未定义的方法,这会导致您的应用程序抛出异常
【讨论】:
詹姆斯您好,感谢您的帮助。 我可以知道如果我没有输入冒号会发生什么吗?没有冒号“addAnnotation”不会被调用? 加一个冒号告诉系统寻找一个带参数的方法。如果没有冒号,它会查找不带任何参数的方法。就系统而言,它们是两种不同的方法——它们的名称可能完全不同。以上是关于添加到 MKMapView 时的 UILongPressGestureRecognizer 问题的主要内容,如果未能解决你的问题,请参考以下文章
Swift - 将 MKAnnotationView 添加到 MKMapView
无法将点击手势识别器添加到 SwiftUI MKMapView UIViewRepresentable