【问题标题】:Multiple MapKit overlay colours from array数组中的多个 MapKit 叠加颜色
【发布时间】:2016-06-12 22:38:55
【问题描述】:

如何让每个泡泡变成不同的颜色?

这是我的颜色数组:

var tableColors = [UIColor(red: 220.0/255.0, green: 95.0/255.0,  blue: 19.0/255.0, alpha: 0.9),  // Rust
    UIColor(red: 255.0/255.0, green: 191.0/255.0, blue: 59.0/255.0, alpha: 0.9),  // Yellow
    UIColor(red: 255.0/255.0, green: 91.0/255.0,  blue: 51.0/255.0, alpha: 0.9),  // Red
    UIColor(red: 0.0/255.0,   green: 160.0/255.0, blue: 174.0/255.0, alpha: 0.9), // KAL
    UIColor(red: 0.0/255.0,   green: 121.0/255.0, blue: 95.0/255.0, alpha: 0.9),  // Green
    UIColor(red: 104.0/255.0, green: 68.0/255.0,  blue: 88.0/255.0, alpha: 0.9),  // Purple
    UIColor(red: 244.0/255.0, green: 97.0/255.0,  blue: 119.0/255.0, alpha: 0.9), // Pink
    UIColor(red: 1.0/255.0,   green: 116.0/255.0, blue: 200.0/255.0, alpha: 0.9), // Blue
    UIColor(red: 0.0/255.0,   green: 188.0/255.0, blue: 111.0/255.0, alpha: 0.9), // Light Green
    UIColor(red: 0.0/255.0,   green: 196.0/255.0, blue: 179.0/255.0, alpha: 0.9), // Aqua
    UIColor(red: 246.0/255.0, green: 50.0/255.0,  blue: 62.0/255.0, alpha: 0.9)] // Hot

这是从 CloudKit 数据创建位置的代码。

func fetchBubble() {

    let query = CKQuery(recordType: "Collection", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))

    publicDB!.performQuery(query, inZoneWithID: nil) { results, error in

        if error == nil {

            for collection in results! {

                let collectionLocation = collection.valueForKey("Location") as? CLLocation

                let collectionName = collection.valueForKey("Name") as! String

                dispatch_async(dispatch_get_main_queue(), { () -> Void in

                    if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) {

                        let intrepidLat: CLLocationDegrees =  (collectionLocation?.coordinate.latitude)!

                        let intrepidLong: CLLocationDegrees = (collectionLocation?.coordinate.longitude)!

                        let title = collectionName

                        let coordinate = CLLocationCoordinate2DMake(intrepidLat, intrepidLong)

                        let regionRadius = 50.0

                        let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,
                            longitude: coordinate.longitude), radius: regionRadius, identifier: title)

                        self.locationManager.startMonitoringForRegion(region)

                        let restaurantAnnotation = MKPointAnnotation()

                        self.mapView.addAnnotation(restaurantAnnotation)

                        restaurantAnnotation.coordinate = coordinate

                        let circle = MKCircle(centerCoordinate: coordinate, radius: regionRadius)

                        self.mapView.addOverlay(circle)

                        self.numberOfObjectsInMyArray()

                    }

                    else {

                        print("System can't track regions")

                    }


                })

            }

        }

        else {

            print(error)

        }

    }

}

然后我用下面的代码画圆圈

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {

    let circleRenderer = MKCircleRenderer(overlay: overlay)

    for index in 1...5 {

        circleRenderer.fillColor = self.tableColors[index + 1]

    }

    return circleRenderer

}

我尝试了几种循环颜色的方法,但目前它正在将数组中的相同颜色应用于每个气泡。

任何帮助将不胜感激!

【问题讨论】:

    标签: arrays swift mapkit overlay cloudkit


    【解决方案1】:

    rendererForOverlay 标注方法中,您为所有 circleRenderer 设置相同的最后一种颜色。所以代替你的代码

    for index in 1...5 {
        circleRenderer.fillColor = self.tableColors[index + 1]
    }
    

    替换为

    circleRenderer.fillColor = self.randomTableColors()
    

    并实现这个方法来给随机颜色如下。

    func randomTableColors() -> UIColor {
        let randomIndex = Int(arc4random_uniform(UInt32(tableColors.count)))
        return tableColors[randomIndex]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      相关资源
      最近更新 更多