有人可以告诉我为什么在声明let = Gradient
时我有错误contectual type CFArray can not be used with array literal
使用此代码 - 这只是一个背景渐变,我认为这比使用矢量更好..
override func drawRect(rect: CGRect) {
//// General Declarations
let context = UIGraphicsGetCurrentContext()
//// Color Declarations
let gradientColor = UIColor(red: 0.180, green: 0.808, blue: 0.831, alpha: 0.196)
let gradientColor2 = UIColor(red: 0.200, green: 0.282, blue: 0.392, alpha: 1.000)
//// Gradient Declarations
let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [gradientColor2.CGColor, gradientColor2.blendedColorWithFraction(0.5, ofColor: gradientColor).CGColor, gradientColor.CGColor], [0, 1, 1])!
//// Rectangle Drawing
let rectanglePath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 375, height: 667))
CGContextSaveGState(context)
rectanglePath.addClip()
CGContextDrawLinearGradient(context, gradient, CGPoint(x: 187.5, y: -0), CGPoint(x: 187.5, y: 667), CGGradientDrawingOptions())
CGContextRestoreGState(context)
}
}
答案 0 :(得分:0)
所以这意味着你不能将swift数组传递给CGGradientCreateWithColors函数。您需要在CFArray中转换该数组,如下所示:
let gradientArray = [gradientColor2.CGColor, gradientColor.CGColor]
let arrayPointer = UnsafeMutablePointer<UnsafePointer<Void>>(gradientArray)
let cfArray = CFArrayCreate(nil, arrayPointer, gradientArray.count, nil)
然后将CFArray引用传递给函数:
let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), cfArray, [0, 1, 1])!