Bagusflyer
Bagusflyer

Reputation: 12915

Issue when I'm trying to draw gradient in swift

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

Upvotes: 7

Views: 9831

Answers (4)

paiv
paiv

Reputation: 5601

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, ...

Upvotes: 13

Fjohn
Fjohn

Reputation: 1592

In my code I had to force unwrap the value of CGColor :

collectionRadient.colors = [ UIColor.redColor().CGColor!,UIColor.purpleColor().CGColor!]

Maybe this can help you ;)...

Upvotes: 0

verec
verec

Reputation: 5294

This might help:

enter image description here

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.

Upvotes: 3

BotMaster
BotMaster

Reputation: 2223

Maybe this?

colors.bridgeToObjectiveC() as CFArray

Upvotes: 0

Related Questions