Ich habe eine Klasse, die eine Unterklasse von NSManagedObject ist, die MKAnnotation
entsprechen und dann, dass ich die Verwendung in einem MapView
, dass einige Orte von CoreData
reihtiOS Swift MapKit warum MKPointAnnotation ziehbar ist, während eine Klasse, die MKAnnotation entspricht nicht
class Location: NSManagedObject , MKAnnotation {
var coordinate : CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: Double(self.latitude), longitude: Double(self.longitude))
}
var title: String? {
return self.name
}
var subtitle: String? {
return self.category
}
}
i fügen Sie dann die abgerufenen Objekte in die MapView
als MKAnnotation
wie die
self.MapView.addAnnotations(self.locations)
und in der viewForAnnotation
machte ich eine Unterklasse von MKAnnotationView
, die ein abgerundetes Imageview wir
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
switch newState {
case .Starting:
view.dragState = .Dragging
case .Ending, .Canceling:
view.dragState = .None
// then i save the changes to CoreData
}
default: break
}
}
die
didChangeDragState
Delegatmethode implementieren sollte sein
draggable
class AMKAnnotationView: MKAnnotationView {
var imageView = UIImageView()
init(annotation: MKAnnotation?, reuseIdentifier: String?, size: CGSize) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
self.frame = CGRectMake(0, 0, size.width, size.height)
self.commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func commonInit() {
self.imageView.frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
self.imageView.layer.masksToBounds = true
self.imageView.layer.cornerRadius = self.imageView.frame.height/2
self.imageView.layer.borderWidth = 5.0
self.imageView.layer.borderColor = UIColor.whiteColor().CGColor
self.imageView.userInteractionEnabled = false
self.addSubview(imageView)
self.sendSubviewToBack(self.imageView)
}
}
dann stelle ich die annotationView
sein draggable
im viewForAnnotation
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? AMKAnnotationView
if annotationView == nil {
annotationView = AMKAnnotationView(annotation: annotation, reuseIdentifier: "pin", size: CGSizeMake(65, 65))
annotationView?.draggable = true
annotationView?.canShowCallout = true
let index = ... // i get the index
let location = ... // i get the current location
annotationView?.imageView.image = UIImage(named: "No Photo")
}
return annotationView
}
, um die annotationView
zu machen hat
Wenn ich versuche, die Annotation auf der Karte zu ziehen, tut dies nicht arbeiten
* Die Lösung, die Ich mag nicht *
Die Art, wie ich es als der Titel dieser Frage arbeiten und es sagt, ist MKPointAnnotation
zu verwenden, und ich meine damit ist jedes Mal, wenn ich ein Add Anmerkung zu der Karte i MKPointAnnotation
umwandeln, die mir eine andere Unterklasse von MKPointAnnotation
machen gemacht, so dass ich den Überblick über die Lage
class AMKPointAnnotation : MKPointAnnotation {
var location : Location!
init(location:Location) {
super.init()
self.location = location
self.title = location.title
self.subtitle = location.subtitle
self.coordinate = location.coordinate
}
}
und dann Zugabe zu den MapView
for location in self.locations {
let pointAnnotation = AMKPointAnnotation(location: location)
self.MapView.addAnnotation(pointAnnotation)
}
hat jemand es vorher versucht? Was mache ich falsch?
In Ihrer 'viewForAnnotation' haben Sie vergessen, dem 'annotationView' eine 'Annotation' zuzuweisen. Dies muss der eingehende 'Annotation'-Parameter sein:' annotationView.annotation = annotation' – matt