Last active
August 29, 2015 14:07
-
-
Save ianhirschfeld/6bb89d3ebc3fbdf79c99 to your computer and use it in GitHub Desktop.
Example of subclassing AngleGradientLayer to allow borders.
This file contains 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
// | |
// AngleGradientBorderLayer.swift | |
// AngleGradientBorderTutorial | |
// | |
import UIKit | |
class AngleGradientBorderLayer: AngleGradientLayer { | |
// Properties | |
var gradientBorderWidth: CGFloat = 1 | |
// Override to add a border shape to AngleGradientLayer. | |
override func drawInContext(ctx: CGContext!) { | |
// Draw a shape that fills the view minus the width of your final border. | |
// This can be any shape you want to make a border out of. | |
// This example draws a circle. | |
let shapePath = UIBezierPath(roundedRect: CGRectInset(bounds, gradientBorderWidth, gradientBorderWidth), cornerRadius: bounds.height / 2) | |
// Copy the path of the shape and turn it into a stroke. | |
let shapeCopyPath = CGPathCreateCopyByStrokingPath(shapePath.CGPath, nil, gradientBorderWidth, kCGLineCapButt, kCGLineJoinBevel, 0) | |
CGContextSaveGState(ctx) | |
// Add the stroked path to the context and clip to it. | |
CGContextAddPath(ctx, shapeCopyPath) | |
CGContextClip(ctx) | |
// Call our super class's (AngleGradientLayer) #drawInContext | |
// which will do the work to create the gradient. | |
super.drawInContext(ctx) | |
CGContextRestoreGState(ctx) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment