gpt4 book ai didi

ios - 使图钉可拖动并长按

转载 作者:行者123 更新时间:2023-11-30 13:35:13 24 4
gpt4 key购买 nike

我花了几个小时尝试让图钉在 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 相关的所有问题,但似乎图钉不会自行拖动。问题可能出在哪里?我已经设置了标题并实现了 MKMapViewDelegate 协议(protocol),但仍然不会拖动。

最佳答案

您是否通过代码或 Storyboard将 map View 的委托(delegate)设置为 View Controller ?

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

关于ios - 使图钉可拖动并长按,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158952/

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