使用CGGradient的PaintCode StyleKit Swift 3.0错误

时间:2016-09-18 04:01:21

标签: swift swift3

StyleKit代码:

static let red: UIColor = UIColor(red: 0.800, green: 0.000, blue: 0.000, alpha: 1.000)
static let blue: UIColor = UIColor(red: 0.200, green: 0.600, blue: 1.000, alpha: 1.000)
static let green: UIColor = UIColor(red: 0.000, green: 0.600, blue: 0.200, alpha: 1.000)
static let yinying: UIColor = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.200) static let redGradient: CGGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [UIColor.whiteColor().CGColor, UIColor.whiteColor().blendedColorWithFraction(0.5, ofColor: StyleKitMarkSix.red).CGColor, StyleKitMarkSix.red.CGColor], [0, 0, 1])!
static let blueGradient: CGGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [UIColor.whiteColor().CGColor, UIColor.whiteColor().blendedColorWithFraction(0.5, ofColor: StyleKitMarkSix.blue).CGColor, StyleKitMarkSix.blue.CGColor], [0, 0, 1])!
static let greenGradient: CGGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [UIColor.whiteColor().CGColor, UIColor.whiteColor().blendedColorWithFraction(0.5, ofColor: StyleKitMarkSix.green).CGColor, StyleKitMarkSix.green.CGColor], [0, 0, 1])!
static let blackGradient: CGGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [UIColor.whiteColor().CGColor, UIColor.whiteColor().blendedColorWithFraction(0.5, ofColor: UIColor.blackColor()).CGColor, UIColor.blackColor().CGColor], [0, 0, 1])!

错误消息:

Contextual type 'CFArray' cannot be used with array literal

我改变了:

static let redGradient: CGGradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: [UIColor.whiteColor().CGColor, UIColor.whiteColor().blendedColorWithFraction(0.5, ofColor: StyleKitMarkSix.red).CGColor, StyleKitMarkSix.red.CGColor] , locations:[0, 0, 1])!

也是错误

1 个答案:

答案 0 :(得分:6)

还有一些事情需要改变:

  1. UIColor.whiteColor()现在是UIColor.white
  2. .CGColor现在是.cgColor
  3. 您需要将颜色数组转换为CFArray。 Swift 3中不再对桥接类型进行隐式转换。
  4. Swift可以推断出redGradient的类型,因此您可以删除: CGGradient
  5. 通过这些更改,代码变为:

    static let redGradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),
        colors: [UIColor.white.cgColor, UIColor.white.blendedColorWithFraction(0.5, ofColor: StyleKitMarkSix.red).cgColor, StyleKitMarkSix.red.cgColor] as CFArray,
        locations: [0, 0, 1])!