2016-06-12 3 views
0

어떻게 각 거품을 다른 색상으로 만듭니 까? 여기 배열의 다중 MapKit 오버레이 색상

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 

} 

다음 코드로 원을 그리 거품의.

도움을 주시면 대단히 감사하겠습니다.

답변

0

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] 
} 
관련 문제