Skip to content

Instantly share code, notes, and snippets.

@jhays
Created February 23, 2015 23:37
Show Gist options
  • Save jhays/c75f83ef82274963198d to your computer and use it in GitHub Desktop.
Save jhays/c75f83ef82274963198d to your computer and use it in GitHub Desktop.
IBDesignable Gradient Button
//
// GradientButton.swift
// practice
//
// Created by Julian Hays on 2/18/15.
// Copyright (c) 2015 DevMode. All rights reserved.
//
import Foundation
import UIKit
@IBDesignable class DMGradientButton : UIButton {
@IBInspectable var startColor: UIColor = UIColor.whiteColor() {
didSet{
setupView()
}
}
@IBInspectable var endColor: UIColor = UIColor.blackColor() {
didSet{
setupView()
}
}
@IBInspectable var isHorizontal: Bool = false {
didSet{
setupView()
}
}
@IBInspectable var roundness: CGFloat = 0.0 {
didSet{
setupView()
}
}
// MARK: Overrides ******************************************
override class func layerClass()->AnyClass{
return CAGradientLayer.self
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
// MARK: Internal functions *********************************
// Setup the view appearance
private func setupView(){
let colors:Array = [startColor.CGColor, endColor.CGColor]
gradientLayer.colors = colors
gradientLayer.cornerRadius = roundness
if (isHorizontal){
gradientLayer.endPoint = CGPoint(x: 1, y: 0)
}else{
gradientLayer.endPoint = CGPoint(x: 0, y: 1)
}
self.setNeedsDisplay()
}
// Helper to return the main layer as CAGradientLayer
var gradientLayer: CAGradientLayer {
return layer as CAGradientLayer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment