ios - 核心图形和渐变

标签 ios swift

有人可以告诉我为什么我在声明 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)



}

}

最佳答案

因此,这意味着您无法将 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])!

关于ios - 核心图形和渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37929086/

相关文章:

ios - 我在 Xcode 7.1 中收到此错误。提交错误报告并附上 */var.. IB 代理诊断

objective-c - 有没有办法获取 siri 的用户名设置?

ios - 在 UITableViewDelegate 方法 heightForRowAtIndexPath 中获取 TableView 单元格属性 :

swift - 无法调用 "isValidEmail",参数错误

ios - 当单元格超出屏幕时,多次调用 UITableView

swift - 使用 NSUndoManager,如何使用 Swift 闭包注册撤销

iphone - 获取 NULL 缓存

iphone - Obj-C,dyld:找不到符号:_OBJC_CLASS _ $ _ NSUbiquitousKeyValueStore

ios - Xcode:将信息从 UITableViewController 传递到 UIViewController

swift - 在 Swift 中使用完成处理程序动画图像