使引脚可拖动并长按

make pin draggable and long click

我已经尝试了几个小时让图钉在 MapKit 中可以拖动,但图钉似乎太顽固了,不想移动。

这是我的代码:

import UIKit
import MapKit

protocol AddCoffeeDelegate {
    func viewController(vc: AddCoffeeViewController, didAddCoffee coffee : Coffee! )
}

class AddCoffeeViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet var mapView: MKMapView!

    @IBOutlet weak var coffeeName: UITextField!

    @IBOutlet weak var coffeeRating: UITextField!

    var coffee: Coffee?
    var delegate: AddCoffeeDelegate?



    ////////////////////////////////////////////////////
    var coreLocationManager = CLLocationManager()
    var locationManager : LocationManager!
    var savedLocation : CLLocation?


    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKPointAnnotation {
            let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

            pinAnnotationView.pinTintColor = UIColor.redColor()
            pinAnnotationView.draggable = true
            pinAnnotationView.canShowCallout = false
            pinAnnotationView.animatesDrop = true

            return pinAnnotationView
        }

        return nil
    }



    func getLocation(){
        locationManager.startUpdatingLocationWithCompletionHandler { (latitude, longitude, status, verboseMessage, error) -> () in
            self.displayLocation(CLLocation(latitude: latitude, longitude: longitude))
        }
    }

    func displayLocation(location: CLLocation){
        mapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), span: MKCoordinateSpanMake(0.05, 0.05)), animated: true)


        let locationPinCoord = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

        let annotation = MKPointAnnotation()

        annotation.title = "My Title"
        annotation.subtitle = "My Subtitle"
        annotation.coordinate = locationPinCoord
        mapView.addAnnotation(annotation)

        savedLocation = location
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status != CLAuthorizationStatus.NotDetermined || status !=  CLAuthorizationStatus.Denied || status != CLAuthorizationStatus.Restricted {
            getLocation()
        }
    }


    ////////////////////////////////////////////////////


    override func viewDidLoad() {
        super.viewDidLoad()


        coreLocationManager.delegate = self
        locationManager = LocationManager.sharedInstance

        let authorizationCode = CLLocationManager.authorizationStatus()

        if authorizationCode == CLAuthorizationStatus.NotDetermined && coreLocationManager.respondsToSelector("requestAlwaysAuthorization") || coreLocationManager.respondsToSelector("requestWhenInUseAuthorization"){
            if NSBundle.mainBundle().objectForInfoDictionaryKey("NSLocationAlwaysUsageDescription") != nil {
                coreLocationManager.requestAlwaysAuthorization()
            }else{
                print("no desscription provided ")
            }
        }else{
            getLocation()
        }

    }

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


    @IBAction func cancel(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }


    @IBAction func save(sender: AnyObject) {

        var createdCoffee = Coffee()
        createdCoffee.name = self.coffeeName.text!
        createdCoffee.rating = Double(self.coffeeRating.text!)!
        createdCoffee.place = savedLocation
        self.coffee = createdCoffee
        self.delegate?.viewController(self, didAddCoffee: self.coffee)
    }




}

我在swift中尝试了所有与mapkit相关的问题,但似乎pin不会拖动itself.Where可能是问题吗?我已经设置了标题并实现了MKMapViewDelegate协议,但是还是拖不进去

您是否在代码中或通过 Storyboard 将 mapview 的委托设置为视图控制器?

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    ...
}