簡體   English   中英

根據注釋的屬性設置注釋的字形文本

[英]Set annotation's glyphtext based on the annotation's properties

我遇到了自定義注釋顯示不正確的問題。 在我的代碼中,我檢查當前注釋是否針對具有給定唯一標識符的電台。 如果是這樣,我會自定義其屬性。

StationAnnotationView.swift

class StationAnnotationView: MKMarkerAnnotationView {

override var annotation: MKAnnotation? {
    willSet {
        guard let station = newValue as? Station else { return }

        clusteringIdentifier = nil
        displayPriority = .required

        if (station.id == "26") {
                glyphText = "p"
                markerTintColor = UIColor(named: "Blue")
        }
    }
}

起初,我的mapView正確顯示注釋(即,更改唯一具有station.id == 26站的顏色和字形文本),但是在平移和縮放一段時間后,我的自定義格式開始應用於其他注釋(這不應該發生,因為任何給定的station.id只有一個站)。 我懷疑這是由於AnnotationView重用了注釋。 我怎樣才能防止這種情況發生?

正如您所說,這是由於 AnnotationView 重用了注釋。 試試下面的代碼:

    if (station.id == "26") {
            glyphText = "p"
            markerTintColor = UIColor(named: "Blue")
    } else {
            glyphText = // Default text
            markerTintColor = // Default color
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM