繁体   English   中英

添加到MKMapView时出现UILongPressGestureRecognizer问题

[英]UILongPressGestureRecognizer issue when adding to MKMapView

我仍然不熟悉Swift,并且正在尝试弄清楚如何在mapview上添加长按手势注释。

但是,我一直收到此错误:

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.
}


}

您只需要在addAnotation之后添加一个冒号:

var uilpgr = UILongPressGestureRecognizer(target: self, action: "addAnotation:")

在您省略冒号的版本中,将调用带有此签名的方法。 注意没有参数

func addAnotation()

因此,您的UILongPressGestureRecognizer试图调用上述方法,该方法未定义,并导致您的应用程序引发异常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM