拖动自定义注释视图
Drag Custom Annotation View
我有我的 CoreData 自定义 Annotation
class:
extension Annotation: MKAnnotation {
public var coordinate: CLLocationCoordinate2D {
let cllCoordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
return cllCoordinate
}
public var title: String? {
return self.objectId
}
class func keyPathsForValuesAffectingCoordinate() -> Set<String> {
return Set<String>([ #keyPath(latitude), #keyPath(longitude) ])
}
@nonobjc public class func fetchRequest() -> NSFetchRequest<Annotation> {
return NSFetchRequest<Annotation>(entityName: "Annotation")
}
@NSManaged public var latitude: Double
@NSManaged public var longitude: Double
@NSManaged public var dateTime: Date
@NSManaged public var type: String
@NSManaged public var objectId: String?
}
结合 fetchedResultsController
添加和删除注释效果很好。但是现在想把一个注解拖到另一个位置。但仅将 isDraggable
设置为 true 并不是全部。我找不到更新的描述如何将其集成。
这是我的 viewFor
方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is Annotation else { return nil }
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.isDraggable = true
annotationView?.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
let customAnno = annotation as! Annotation
let image = UIImage(named: customAnno.type)
annotationView!.image = image
return annotationView
}
我还需要什么?我想按住注释,按住并转到另一个位置,然后松开手指,注释就停留在那里。
拖动时,需要改变底层注解的coordinate
。如果它是只读的,它就不能这样做。所以,如果你想让它可以拖动,你必须给 coordinate
属性 一个 setter:
public var coordinate: CLLocationCoordinate2D {
get {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
set {
latitude = newValue.latitude
longitude = newValue.longitude
}
}
我有我的 CoreData 自定义 Annotation
class:
extension Annotation: MKAnnotation {
public var coordinate: CLLocationCoordinate2D {
let cllCoordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
return cllCoordinate
}
public var title: String? {
return self.objectId
}
class func keyPathsForValuesAffectingCoordinate() -> Set<String> {
return Set<String>([ #keyPath(latitude), #keyPath(longitude) ])
}
@nonobjc public class func fetchRequest() -> NSFetchRequest<Annotation> {
return NSFetchRequest<Annotation>(entityName: "Annotation")
}
@NSManaged public var latitude: Double
@NSManaged public var longitude: Double
@NSManaged public var dateTime: Date
@NSManaged public var type: String
@NSManaged public var objectId: String?
}
结合 fetchedResultsController
添加和删除注释效果很好。但是现在想把一个注解拖到另一个位置。但仅将 isDraggable
设置为 true 并不是全部。我找不到更新的描述如何将其集成。
这是我的 viewFor
方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is Annotation else { return nil }
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.isDraggable = true
annotationView?.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
let customAnno = annotation as! Annotation
let image = UIImage(named: customAnno.type)
annotationView!.image = image
return annotationView
}
我还需要什么?我想按住注释,按住并转到另一个位置,然后松开手指,注释就停留在那里。
拖动时,需要改变底层注解的coordinate
。如果它是只读的,它就不能这样做。所以,如果你想让它可以拖动,你必须给 coordinate
属性 一个 setter:
public var coordinate: CLLocationCoordinate2D {
get {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
set {
latitude = newValue.latitude
longitude = newValue.longitude
}
}