gpt4 book ai didi

arrays - 数组中的多个 MapKit 叠加颜色

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

如何使每个气泡具有不同的颜色?

这是我的颜色数组:

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

}

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

任何帮助将不胜感激!

最佳答案

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]
}

关于arrays - 数组中的多个 MapKit 叠加颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37779764/

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