GMSPolyline允许设置strokeColor
,但颜色的alpha分量被忽略,并且类及其任何超类都没有alpha
或opacity
属性。
我错过了什么吗?是否无法添加非不透明叠加层?
如果你只有一个六角形,你应该使用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)