Created
August 7, 2014 12:02
-
-
Save chrismiles/e7e10c768532164159e4 to your computer and use it in GitHub Desktop.
Core Graphics gradient drawing with Swift? No problem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let colorSpace = CGColorSpaceCreateDeviceRGB() | |
let componentCount : UInt = 4 | |
let components : [CGFloat] = [ | |
0, 0, 0, 0, | |
1.0, 1.0, 1.0, 1.0, | |
1.0, 1.0, 1.0, 1.0, | |
0, 0, 0, 0 | |
] | |
let locations : [CGFloat] = [0, 0.25, 0.75, 1.0] | |
let gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, componentCount) | |
let startPoint = CGPoint(x: 0, y: 0) | |
let endPoint = CGPoint(x: CGRectGetWidth(bounds), y: 0) | |
CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Swift 2 update:
let context = UIGraphicsGetCurrentContext() // <-- and you missed the context
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, CGGradientDrawingOptions(rawValue: 0))
/*
Have been struggling with this for too long. Going to buy Paintcode instead!
*/