Ios Swift 4 Mapkit注释与xib文件的自定义调用
我正在尝试在swift 4下创建自定义注释标注。我花了几天时间在谷歌上搜索,我能找到的例子不是swift 2就是swift 3,有些部分我不懂 我已经创建了一个xib视图文件及其控制器:Ios Swift 4 Mapkit注释与xib文件的自定义调用,ios,swift,mapkit,swift4,mapkitannotation,Ios,Swift,Mapkit,Swift4,Mapkitannotation,我正在尝试在swift 4下创建自定义注释标注。我花了几天时间在谷歌上搜索,我能找到的例子不是swift 2就是swift 3,有些部分我不懂 我已经创建了一个xib视图文件及其控制器: import UIKit import MapKit class ForHireAnnotationCallOut: UIView { @IBOutlet weak var coImage: UIImageView! @IBOutlet weak var coName: UILabel! @IBOutlet
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 annotation函数来使用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
}
func映射视图(\mapView:MKMapView,viewforannotation:myAnnotation)->MKAnnotationView?{
//如果批注是用户的位置,则不希望显示自定义图像。
guard!(注释为MKUserLocation)else{
归零
}
//最好使这个类成为属性
让annotationIdentifier=“annotationIdentifier”//我不会使用UIView+Xib。@在这种情况下,你有什么建议?我觉得我可能不在正确的兔子洞里。从MKPointAnnotation派生你自己的自定义注释只需使用你的UIViewController
。除此之外,你似乎走上了正确的道路。