Skip to content

Instantly share code, notes, and snippets.

@ianhirschfeld
Last active August 29, 2015 14:07
Show Gist options
  • Save ianhirschfeld/6bb89d3ebc3fbdf79c99 to your computer and use it in GitHub Desktop.
Save ianhirschfeld/6bb89d3ebc3fbdf79c99 to your computer and use it in GitHub Desktop.
Example of subclassing AngleGradientLayer to allow borders.
//
// 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