当我尝试在Swift代码中绘制渐变时出现错误:
GradientView.swift:31:40:找不到接受提供参数的'__conversion'的重载
这是我的代码:
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的文档中找不到任何内容。任何的想法?感谢
答案 0 :(得分:11)
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, ...
答案 1 :(得分:3)
这可能会有所帮助:
import CoreGraphics
import Foundation
import UIKit
class GradientLabel : UILabel {
let gradient: CGGradient!
let alignedToSuperview: Bool!
init(colors: [UIColor]!, locations: [CGFloat]!, alignedToSuperview: Bool = true) {
super.init(frame: CGRect.zeroRect)
let gradientColors = colors.map {(color: UIColor!) -> AnyObject! in return color.CGColor as AnyObject! } as NSArray
let colorSpace = CGColorSpaceCreateDeviceRGB()
self.gradient = CGGradientCreateWithColors(colorSpace, gradientColors, locations)
self.alignedToSuperview = alignedToSuperview
}
override func drawTextInRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context) ;
CGContextSetTextDrawingMode(context, kCGTextFill) ;
super.drawTextInRect(rect) ;
let lines = CGRect (origin: CGPoint(x: 0, y:16.0), size: CGSize(width: 160.0, height: 2.0)) ;
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor) ;
CGContextFillRect(context, lines) ;
// snapshot the context into the just created mask
let alphaMask = CGBitmapContextCreateImage(context) ;
CGContextClearRect(context, rect) ;
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextClipToMask(context, rect, alphaMask);
let startPoint = CGPoint(x:0.0, y: 0.0)
let endPoint = CGPoint(x:self.superview.bounds.size.width, y: 0.0)
if self.alignedToSuperview {
CGContextTranslateCTM(context, -1.0 * self.frame.origin.x, 0.0)
}
// var options: CGGradientDrawingOptions = kCGGradientDrawsBeforeStartLocation as CGGradientDrawingOptions
// | kCGGradientDrawsAfterEndLocation GGradientDrawsBeforeStartLocation as UInt32 ;
// CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, options) ;
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 3)
CGContextRestoreGState(context)
}
}
也分享为:https://gist.github.com/verec/238ee65968321c0e78a3,显示示例用途。
答案 2 :(得分:1)
在我的代码中,我不得不强行打开CGColor的值:
collectionRadient.colors = [ UIColor.redColor().CGColor!,UIColor.purpleColor().CGColor!]
也许这可以帮助你;)...
答案 3 :(得分:0)
也许这个?
colors.bridgeToObjectiveC() as CFArray