ios - XCode 6 和 Swift 中的 MKOverlay 颜色不匹配

标签 ios xcode swift ios8 mapkit

使用 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值?感谢帮助

最佳答案

添加此扩展以避免在需要时反复键入 /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)

关于ios - XCode 6 和 Swift 中的 MKOverlay 颜色不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27272921/

相关文章:

iOS)如何更改已添加 subview 的位置

ios - 使用自动布局创建的 UILabel 不显示 NSMutableAttributedString 的属性

iphone - 在 ios 版本高于 xcode 的 sdk 的 iphone 上测试应用程序

ios - 如何自定义单元格的一部分

iOS swift : How to prepare a cell for reuse

json - 将 JSON 数据返回给 Swift 中的调用类

ios - 在显示之前将 ViewController 加载到 ContainerView 中,因此没有可见的过渡

ios - 每当用户进入/退出区域时通知后端

ios - ITC.apps.assetvalidation.BITCODE_IMBALANCE_ERROR.error.message

ios - 何时需要将应用程序源包含在测试目标中?