Я пытаюсь создать настраиваемую выноску аннотации под Swift 4. Я провел последние несколько дней в Google, примеры, которые я смог найти, были либо Swift 2, либо 3, и я не мог понять некоторые части.
Я создал файл представления xib и его контроллер:
import UIKit
import MapKit
class ForHireAnnotationCallOut: UIView {
@IBOutlet weak var coImage: UIImageView!
@IBOutlet weak var coName: UILabel!
@IBOutlet weak var coServiceType: UILabel!
@IBOutlet weak var coExp: UILabel!
@IBOutlet weak var coHourlyRate: UILabel!
@IBAction func ocDetailsBtn(_ sender: Any) {
}
var coAnnotation = MKPointAnnotation()
internal var coData: User?{
didSet{
coName.text = coData?.name
coServiceType.text = coData?.serviceType
coExp.text = (coData?.exp.replacingOccurrences(of: "Years", with: "Yrs"))! + " Exp."
coHourlyRate.text = coData?.hourlyRate
self.layoutIfNeeded()
}
}
internal var preferredContentSize:CGSize{
return CGSize(width: 0, height: 0)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
Структура пользователя содержит:
public struct User: Equatable {
var uid: String
var name: String
var phone: String
var serviceType: String
var exp: String
var hourlyRate: String
var pictureUrl: String
}
Пользовательский класс аннотации:
public class myAnnotation: NSObject, MKAnnotation {
public var coordinate: CLLocationCoordinate2D
public var title: String?
public var subtitle: String?
var userKey: String?
public var exp: String?
public var hourlyRate: String?
override init() {
self.coordinate = CLLocationCoordinate2D()
self.title = nil
self.subtitle = nil
self.userKey = nil
self.exp = nil
self.hourlyRate = nil
}
}
Насколько я понимаю, для того, чтобы иметь настраиваемый вызов, вам нужно будет использовать функцию аннотации mapView viewFor для использования представления xib.
Я нашел пример из другого потока от @Babac и изменил его на что-то вроде этого в Swift 4:
func mapView(_ mapView: MKMapView, viewFor annotation: myAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !(annotation is MKUserLocation) else {
return nil
}
// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier" //<---not very sure why we need this
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}
return annotationView
}
Кроме annotationIdentifier, я не понимаю, где мне следует включить свое представление xib и его контроллер в эту функцию для вызова. Также как передать значения в контроллер представления xib?
Спасибо
UIViewController
. В остальном, похоже, вы на правильном пути. - person El Tomato   schedule 21.06.2018