Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 459
Bagusflyer Avatar asked Jun 09 '14 03:06

Bagusflyer


People also ask

How do you apply a linear gradient in Swift?

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.


2 Answers

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, ...
like image 149
paiv Avatar answered Sep 24 '22 01:09

paiv


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.

like image 36
verec Avatar answered Sep 23 '22 01:09

verec