ios - 如何为 MKAnnotationView 使用自定义 XIB?
问题描述
我想使用自定义 XIB 来代替 MKAnnotation View,但我在网上找不到任何可以帮助我的资源。需要明确的是,我不想要标注,我不想要图像代替图钉,我想用 xib 完全替换图钉。作为参考,我希望我的别针看起来像这样 -
https://i.stack.imgur.com/gbdFI.png
这将是图钉设计,中间的图像会为所有用户更改。我从来没有在 MKMapView 上工作过,在观看了一些关于它的教程后,我刚刚在地图上显示了 MKPinAnnotationView。谁能指导我如何实现这一目标
编辑:使用 Alexander Nikolaychuk 的链接,我能够使用 XIB 创建 MKPinAnnotationViews。
作为参考,这是对我有用的代码:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
if (annotation is MKUserLocation) {
return nil
} else {
let annotationIdentifier = "CustomPinAnnotation"
let nibName = "CustomPinAnnotation"
let viewFromNib = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as! CustomPinAnnotation
var annotationView: CustomPinAnnotation?
// if there is a view to be dequeued, use it for the annotation
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? CustomPinAnnotation {
if dequeuedAnnotationView.subviews.isEmpty {
dequeuedAnnotationView.addSubview(viewFromNib)
}
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
} else {
// if no views to dequeue, create an Annotation View
let av = CustomPinAnnotation(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.addSubview(viewFromNib)
annotationView = av // extend scope to be able to return at the end of the func
}
// after we manage to create or dequeue the av, configure it
if let annotationView = annotationView {
annotationView.canShowCallout = true // callout bubble
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
let customView = annotationView.subviews.first as! CustomPinAnnotation
customView.frame = annotationView.frame
customView.profilePicImageView.image = UIImage(named: "dummy4")
}
return annotationView
}
}
现在,我的应用程序中还剩下 2 个问题
我有我想在地图上以数组模式显示的所有数据。如何使用模式中的个人资料图片 URL 并将其显示到注释视图中的 imageView 上?由于 viewFor 注释不会迭代并且没有任何索引值,因此在这里传递我的模态对我来说具有挑战性。
即使我的 xIB 看起来像这样: https ://i.stack.imgur.com/Wg4ZU.png
在 MapView 上看起来像这样:https ://i.stack.imgur.com/e7CAr.jpg
解决方案
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
if (annotation is MKUserLocation) {
return nil
} else {
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKPinAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationIdentifier") as? MKPinAnnotationView {
//Here you can setup
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
}
您不需要自己初始化。它使用自定义的 CollectionViewCells。尝试使用此示例。(这是一个委托功能仅供参考)
推荐阅读
- jpeg - 需要帮助修改和编写 JPG 图像
- visual-studio - 如何使用 Visual Studio 2017+ 删除 GitHub 项目中未使用的 .NET 框架/核心引用?
- javascript - 如何将变量分配给回调?
- python - python上的太空侵略者游戏
- javascript - 我可以创建一个前端 JavaScript 应用程序来触发对另一个网页的隐藏请求以从中抓取数据吗?
- python - VS Code:找不到用于 ipykernel 启动的内核规范
- typescript - vim 标记的模板文字语法区域在打字稿中不起作用
- pattern-matching - 使用 python POO 进行图挖掘:创建图并测试匹配
- node.js - 在电子反应应用程序中导入 whatsapp-web.js nodejs 模块的问题
- windows - 我可以获得适用于 Windows 32 位的 MongoDB Shell 吗?