ios - 如何通过子类将自定义图像添加到注释 - Swift 3.0

标签 ios swift xcode viewcontroller

我想通过子类为标注注释添加自定义图像(之前的问题不包括使用子类)

到目前为止,这是我的代码:

名为“Capital.swift”的子类

import MapKit
import UIKit

class Capital: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var info: String

    // here we would add the custom image

    init(title: String, coordinate: CLLocationCoordinate2D, info: String) {
        self.title = title
        self.coordinate = coordinate
        self.info = info

     // add additional lines as needed

    }
}

这是我的 ViewController.swift 代码

import MapKit
import UIKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var mapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.")
        let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.")
        let paris = Capital(title: "Paris", coordinate: CLLocationCoordinate2D(latitude: 48.8567, longitude: 2.3508), info: "Often called the City of Light.")
        let rome = Capital(title: "Rome", coordinate: CLLocationCoordinate2D(latitude: 41.9, longitude: 12.5), info: "Has a whole country inside it.")
        let washington = Capital(title: "Washington DC", coordinate: CLLocationCoordinate2D(latitude: 38.895111, longitude: -77.036667), info: "Named after George himself.")

        mapView.addAnnotations([london, oslo, paris, rome, washington])
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        let identifier = "Capital"
        if annotation is Capital {
            if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
                annotationView.annotation = annotation
                return annotationView
            } else {
                let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
                annotationView.isEnabled = true
                annotationView.canShowCallout = true

                let btn = UIButton(type: .detailDisclosure)
                annotationView.rightCalloutAccessoryView = btn
            return annotationView
         }
       }
        return nil
    }

这里我们将自定义图片添加到annotationView

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        let capital = view.annotation as! Capital
        let placeName = capital.title
        let placeInfo = capital.info

        // Add custom image

        let SecondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
        self.show(SecondViewController!, sender: nil)       
    }
}

谢谢

最佳答案

map View 的委托(delegate),而不是注释对象本身,决定了注释的注释 View 。因此,您需要在 map View 委托(delegate)方法的实现中设置图像:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

如果需要,您的 Capitol 类可以提供正确的图像,例如:

class Capital: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var info: String
    var imageForAnnotationView: UIImage? {
        guard let title = title else { return nil }
        return UIImage(named: "\(title).jpg")
    }
…

您可以像这样重写您的委托(delegate)实现并获取图像:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let identifier = "Capital"
    guard let annotation = annotation as? Capital else { return nil }

    let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) ?? MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)

    annotationView.annotation = annotation
    annotationView.isEnabled = true
    annotationView.canShowCallout = true
    annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

    // set the image to the annotation view
    annotationView.image = annotation.imageForAnnotationView

    return annotationView
}

关于ios - 如何通过子类将自定义图像添加到注释 - Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450536/

相关文章:

ios - Swift didSet 已调用但未更新 UILabel - iOS 属性观察器

ios - 如何在 UIWebview 中包含自定义字体?

ios - AWS Cognito 匿名/ guest 用户的 IdentityId 更改

swift - Swift 常量默认是惰性的吗?

ios - 如何快速在 sceneKit 中连续闪烁 3d 对象?

xcode - 每次启动时都会验证 Xcode beta 6.2

ios - xcode项目组织

ios - uiwebview请求集httpBody

macos - 在 OSX 中设置多行标签的高度

ios - 如何在 OS EL Capitan 10.11.1 中运行 Xcode 5.1.1?