根据布尔属性更新mapView引脚颜色

问题描述 投票:0回答:1

我正在尝试根据是否访问过热点来更新mapView引脚的颜色。

请参阅下面的代码。下面是订阅MKAnnotation代表的热点类。确定引脚颜色的属性是访问过的Bool。

init(title: String, hotspotName: String,coordinate: CLLocationCoordinate2D, ARType:String, hotspotId:String)
{
    //assing values to properties
    self.hotspotTitle = title
    self.hotspotName = hotspotName
    self.ARType = ARType
    self.coordinate = coordinate
    self.hotspotId = hotspotId
    super.init()
}

var visited: Bool {
    return self.completed
}

var title: String?
{
    return hotspotTitle
}

var subtitle: String?
{

    return hotspotName
}

下面是我用来尝试更新引脚颜色的mapKit委托方法。

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

    // check if annotation is of type Hotspot, as we dont want to alter other types of annotations
    guard annotation is Hotspot else {
        return  nil
    }

    let identifier = "Hotspot"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    let pinView = MKPinAnnotationView(annotation:annotation,reuseIdentifier: identifier)
    if annotationView == nil
    {

        if let hotspot = annotationView?.annotation as? Hotspot {
            if !hotspot.visited
            {

                pinView.pinTintColor = UIColor(red:0.32, green:0.82, blue:0.4, alpha:1)
                pinView.tintColor = UIColor(white: 0.0, alpha: 0.5)
            }
            else
            {
                pinView.pinTintColor = UIColor(red:0.0, green:0.0, blue:0.90, alpha:1)
                pinView.tintColor = UIColor(white: 0.0, alpha: 0.5)


            }
        }


        pinView.frame = mapView.frame
        pinView.isEnabled = true
        pinView.canShowCallout = true
        pinView.animatesDrop = true
        let rightButton = UIButton(type: .detailDisclosure)
        rightButton.addTarget(self,action: #selector(getDirections),for: .touchUpInside)
        pinView.rightCalloutAccessoryView = rightButton
        annotationView = pinView

    }

    if let annotationView = annotationView
    {

        annotationView.annotation = annotation


        // 5
        let button = annotationView.rightCalloutAccessoryView as! UIButton
        if let index = trail.hotspots.index(of: annotation as! Hotspot)
        {

            button.tag = index
        }
    }

    return annotationView
}
ios swift mapkit mapkitannotation
1个回答
0
投票

if annotationView == nil语句中更新mapView引脚颜色。

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

    // check if annotation is of type Hotspot, as we dont want to alter other types of annotations
    guard annotation is Hotspot else {
        return  nil
    }

    let identifier = "Hotspot"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    let pinView = MKPinAnnotationView(annotation:annotation,reuseIdentifier: identifier)
    if annotationView == nil
    {
        pinView.frame = mapView.frame
        pinView.isEnabled = true
        pinView.canShowCallout = true
        pinView.animatesDrop = true
        let rightButton = UIButton(type: .detailDisclosure)
        rightButton.addTarget(self,action: #selector(getDirections),for: .touchUpInside)
        pinView.rightCalloutAccessoryView = rightButton
        annotationView = pinView
    }

    if let hotspot = annotationView?.annotation as? Hotspot {
        if !hotspot.visited
        {
            annotationView.pinTintColor = UIColor(red:0.32, green:0.82, blue:0.4, alpha:1)
            annotationView.tintColor = UIColor(white: 0.0, alpha: 0.5)
        }
        else
        {
            annotationView.pinTintColor = UIColor(red:0.0, green:0.0, blue:0.90, alpha:1)
            annotationView.tintColor = UIColor(white: 0.0, alpha: 0.5)
        }
    }

    if let annotationView = annotationView
    {

        annotationView.annotation = annotation


        // 5
        let button = annotationView.rightCalloutAccessoryView as! UIButton
        if let index = trail.hotspots.index(of: annotation as! Hotspot)
        {

            button.tag = index
        }
    }

    return annotationView
}
© www.soinside.com 2019 - 2024. All rights reserved.