如何更改GMSPolyline的不透明度

时间:2015-12-31 14:37:39

标签: google-maps google-maps-sdk-ios

GMSPolyline允许设置strokeColor,但会忽略颜色的alpha分量,并且该类及其任何超类都不具有alphaopacity属性。

我错过了什么吗?是否无法添加非不透明叠加层?

1 个答案:

答案 0 :(得分:0)

如果只有十六进制字符串,则应使用this答案来转换颜色。

我遇到了一个处理6个字符加一个CGFloat的alpha值的十六进制字符串的问题。

因此,我对https://stackoverflow.com/a/33397427/4420355的扩展名做了一些更改。

 extension UIColor {
  public convenience init?(hexString: String, alpha: CGFloat) {
    var newHexString = hexString

    if !hexString.hasPrefix("#") {
        newHexString += "#"
    }

    if newHexString.count != 7 {
        print("Invalid input - hex must have 6 characters: your hexstring \(newHexString) has \(newHexString.count) characters")
        return nil
    }

    let hex = newHexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
    var initInt32 = UInt32()
    Scanner(string: hex).scanHexInt32(&initInt32)
    let r, g, b: UInt32
    switch hex.count {
    case 6: // RGB (24-bit)
        (r, g, b) = (initInt32 >> 16, initInt32 >> 8 & 0xFF, initInt32 & 0xFF)
    default:
        (r, g, b) = (0, 0, 0)
    }
    self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: alpha)
 }
}

之后,您可以这样称呼

gmsPolygon?.strokeColor = UIColor(hexString: "#aaaaaa", withAlpha: 0.2)
gmsPolygon?.strokeColor = UIColor(hexString: "aaaaaa", withAlpha: 0.2)