2016-03-22 50 views
0

我一直在嘗試幾個小時,以便在MapKit中可以拖動這個引腳,但是看起來這個引腳是如此固執以至於它不想移動。make pin draggable and long click

這是我的代碼:

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




} 

我都想盡相關的問題在迅速mapkit,但似乎該引腳不會拖itself.Where可問題是什麼?我已經設置了標題並實現了MKMapViewDelegate協議,但它仍然不會拖動。

回答

0

您是通過代碼還是通過Storyboard將mapview的委託設置爲視圖控制器?

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

OMG我並沒有意識到!!!!!!!!!!!!超thnx人!!!!!! –

+0

不客氣:) –