【问题标题】:MKOverlay color mismatch in XCode 6 and SwiftXCode 6 和 Swift 中的 MKOverlay 颜色不匹配
【发布时间】:2014-12-03 21:53:13
【问题描述】:

使用 UIColor 设置 MKOverlayPathRenderer.fillColor 会显示错误的颜色。

例子:

对于 RGB 0,255,0 应该显示绿色。按预期工作:

 func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
{
    if overlay is MKCircle {
        var circle = MKCircleRenderer(overlay: overlay)
        circle.fillColor = UIColor(red: 0, green: 255, blue: 0, alpha: 0.5)
        return circle
    } else {
        return nil
    }
}

结果: http://i.imgur.com/f0U3s9L.png

所以我现在尝试设置特定的颜色,接近青色并呈现白色。

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
{
    if overlay is MKCircle {
        var circle = MKCircleRenderer(overlay: overlay)
        circle.fillColor = UIColor(red: 43, green: 229, blue: 227, alpha: 1)
        return circle
    } else {
        return nil
    }
}

结果: http://i.imgur.com/8ZbVjcJ.png

我错过了什么吗?我将如何获得我想要的 RGB 值?感谢帮助

【问题讨论】:

  • RGB 值应该是从 0 到 1。尝试(43.0/255.0) 而不是43 等。
  • 非常感谢,辛苦了

标签: ios xcode swift ios8 mapkit


【解决方案1】:

添加此扩展程序以避免在需要时一遍又一遍地输入/255.0

extension UIColor {

    convenience init(R r: Int, G g: Int, B b:Int, A a:CGFloat) {
        self.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: a)
    }

    convenience init(R r: Int, G g: Int, B b:Int) {
        self.init(R: r, G: g, B: b, A: 1.0)
    }
}

那么你很适合:

circle.fillColor = UIColor(R: 0, G: 255, B: 0, A: 0.5)

circle.fillColor = UIColor(R: 43, G: 229, B: 227)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多