尝试在 Swift 代码中绘制渐变时出现错误:
GradientView.swift:31:40: Could not find an overload for '__conversion' that accepts the supplied arguments
这是我的代码:
let context: CGContextRef = UIGraphicsGetCurrentContext()
let locations: CGFloat[] = [ 0.0, 0.25, 0.5, 0.75 ]
let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor,UIColor.blueColor().CGColor, UIColor.yellowColor().CGColor]
let colorspace: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
let gradient: CGGradientRef = CGGradientCreateWithColors(colorspace, colors, locations)
//CGGradientCreateWithColors(colorspace,colors,locations)
let startPoint: CGPoint = CGPointMake(0, 0)
let endPoint: CGPoint = CGPointMake(500,500)
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
问题是 CGGradientCreateWithColors 采用 CFArray 而不是普通的 Swift 数组。我不知道如何将 CFArray 转换为 Array,并且在 Apple 的文档中找不到任何内容。任何想法?谢谢
最佳答案
swift 3
let colors = [UIColor.red.cgColor, UIColor.green.cgColor,
UIColor.blue.cgColor, UIColor.yellow.cgColor
] as CFArray
swift 2
您可以使用显式类型 CFArray 注释常量:
let colors: CFArray = [UIColor.redColor().CGColor, ...
关于ios - 当我尝试快速绘制渐变时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24113239/