ios - 当我尝试快速绘制渐变时出现问题

标签 ios core-graphics swift

尝试在 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/

相关文章:

ios - 使用 Draw(_ rect : CGRect) 无法获得良好的性能

ios - 在 CarbonTapSwipeNavigation 上应用渐变色

ios - 取消隐藏 UIStackView 元素时的动画方向

ios - 如何正确检查 iOS 版本控制(来自 iOS 7.0)

ios - 如何使用注释创建 PDF?

iphone - 获取在 iOS UIFont 中追踪字符的路径

ios - meteor + Cordova + iOS : unallowed to upload file

iphone - 根据手势重新排列 UIIview

swift - 可选绑定(bind)的参与者的姓名是什么?

swift - 有人可以告诉我是否有我想快速执行的语法