gpt4 book ai didi

ios - 如何创建自定义引脚注释 View

转载 作者:行者123 更新时间:2023-11-30 13:11:01 24 4
gpt4 key购买 nike

我有一张 map ,其中填充了餐车列表,并且我想在选择注释时实现如下所示的自定义标注。目前,我已经使用标题和副标题实现了标准注释标注。

我已经阅读了一些内容,猜测它需要一个自定义的 .xib 文件,但我对此一无所知,因为我最近才开始为 iOS 进行开发。我将如何创建此自定义弹出窗口,以及如何使其在单击时执行导航操作?

谢谢!

enter image description here

最佳答案

首先要更改您需要使用的 pin 图像 viewForAnnotation方法MKMapViewDelegate并设置image MKAnnotationView的属性(property)还可以显示您自己的 View 集canShowCallout属性至false .

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation.isKindOfClass(MKPointAnnotation.classForCoder())) {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("AnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
}
else {
annotationView!.annotation = annotation
}
annotationView!.image = UIImage(named: "Marker")
annotationView!.canShowCallout = false
return annotationView
}
return nil
}

现在要显示您自己的 View ,请使用 didSelectAnnotationView方法MKMapViewDelegate .

    func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
mapView.deselectAnnotation(view.annotation, animated: true)

let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("AnnotationInfoViewController") as! AnnotationInfoViewController

//If you want to show view from XIB file you can create viewController instance like this
//let viewController = UIViewController()
//let myView = NSBundle.mainBundle().loadNibNamed("AnnotationInfoView", owner: self, options: nil)[0] as! AnnotationInfoView
//viewController.view = myView

//Pass the information that you want to show
viewController.data = passdata

//Set the viewController for popoverPresentationController
viewController.modalPresentationStyle = .Popover
viewController.preferredContentSize = CGSize(width: viewController.view.frame.size.width, height: 80)
viewController.popoverPresentationController!.delegate = self;
viewController.popoverPresentationController!.permittedArrowDirections = [.Up , .Down]
viewController.popoverPresentationController!.sourceView = view.superview!
viewController.popoverPresentationController!.sourceRect = view.frame
self.presentViewController(viewController, animated: true, completion: nil)
}

现在显示弹出 View 工具 adaptivePresentationStyleForPresentationController方法UIPopoverPresentationControllerDelegate .

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None;
}

注意:现在,如果您想处理此弹出窗口的点击事件,您可以使用 UITapGestureRecognizer .

关于ios - 如何创建自定义引脚注释 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38734882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com