I got an error when I was trying to draw gradient in Swift code:
GradientView.swift:31:40: Could not find an overload for '__conversion' that accepts the supplied arguments
Here is my code:
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);
The problem is the CGGradientCreateWithColors takes CFArray not a normal Swift Array. I have no idea how to convert CFArray to Array and can't find anything in Apple's document. Any idea? Thanks
Linear Gradient Creating a layer with gradient colors is quite simple. For the most basic gradient, it only requires the colors you want to use, and then you can optionally adjust color location. Once you've created your gradient, add it simply to your view layer by calling the addSublayer function.
Swift 3
let colors = [UIColor.red.cgColor, UIColor.green.cgColor,
UIColor.blue.cgColor, UIColor.yellow.cgColor
] as CFArray
Swift 2
You can annotate constant with explicit type CFArray:
let colors: CFArray = [UIColor.redColor().CGColor, ...
This might help:
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)
}
}
Also shared as: https://gist.github.com/verec/238ee65968321c0e78a3 which shows a sample use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With