Skip to content

Commit

Permalink
Added Inspectable UIButton
Browse files Browse the repository at this point in the history
  • Loading branch information
melvitax committed Jul 22, 2016
1 parent 450eaad commit d076e4b
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
176 changes: 176 additions & 0 deletions AFViewHelper/AFInspectableButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//
// AFInspectableButton.swift
// News Proto
//
// Created by Melvin Rivera on 6/28/16.
// Copyright © 2016 NBC News. All rights reserved.
//

import Foundation
import Foundation
import UIKit
import QuartzCore

@IBDesignable public class AFInspectableButton : UIButton {


// MARK: Border

/**
The layer border color
*/
@IBInspectable override var borderColor: UIColor {
get {
return layer.borderColor == nil ? UIColor.clearColor() : UIColor(CGColor: layer.borderColor!)
}
set {
layer.borderColor = newValue.CGColor
}
}

/**
The layer border width
*/
@IBInspectable override var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}


// MARK: Corner Radius

/**
The layer corner radius
*/
@IBInspectable override var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
setupView()
}
}


// MARK: Shadow

/**
The shadow color of the layer
*/
@IBInspectable override var shadowColor: UIColor {
get {
return layer.shadowColor == nil ? UIColor.clearColor() : UIColor(CGColor: layer.shadowColor!)
}
set {
layer.shadowColor = newValue.CGColor
}
}


/**
The shadow offset of the layer
*/
@IBInspectable override var shadowOffset:CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}

/**
The shadow opacity of the layer

- Returns: Float
*/
@IBInspectable override var shadowOpacity:Float {
get {
return layer.shadowOpacity
}
set {
layer.shadowOpacity = newValue
}
}

/**
The shadow radius of the layer

- Returns: CGFloat
*/
@IBInspectable override var shadowRadius:CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}

// MARK: Inspectable properties

@IBInspectable var startColor: UIColor? {
didSet{
setupView()
}
}

@IBInspectable var endColor: UIColor? {
didSet{
setupView()
}
}

@IBInspectable var isHorizontal: Bool = false {
didSet{
setupView()
}
}


// MARK: Internal functions

private func setupView(){
guard (startColor != nil) && (endColor != nil) else {
return
}
let colors = [startColor!.CGColor, endColor!.CGColor]
gradientLayer.colors = colors
gradientLayer.cornerRadius = cornerRadius
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
}


// MARK: Overrides ******************************************

override public class func layerClass()->AnyClass{
return CAGradientLayer.self
}

override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}

required public init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setupView()
}

}
4 changes: 4 additions & 0 deletions UIiewHelper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
65889B711965051C0049740E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65889B701965051C0049740E /* AppDelegate.swift */; };
65889B731965051C0049740E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65889B721965051C0049740E /* ViewController.swift */; };
65889B781965051C0049740E /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 65889B771965051C0049740E /* Images.xcassets */; };
658A3B161D42BF210029B4E4 /* AFInspectableButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 658A3B151D42BF210029B4E4 /* AFInspectableButton.swift */; };
65D0169E19771DE2005220FB /* AFViewEffects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65D0169D19771DE2005220FB /* AFViewEffects.swift */; };
65D4382B1BB336CF005D1845 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 65D4382A1BB336CF005D1845 /* Launch Screen.storyboard */; };
B023F5541C050EAC0066C617 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B023F5531C050EAC0066C617 /* Main.storyboard */; };
Expand All @@ -31,6 +32,7 @@
65889B701965051C0049740E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = Demo/AppDelegate.swift; sourceTree = "<group>"; };
65889B721965051C0049740E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ViewController.swift; path = Demo/ViewController.swift; sourceTree = "<group>"; };
65889B771965051C0049740E /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = Demo/Images.xcassets; sourceTree = "<group>"; };
658A3B151D42BF210029B4E4 /* AFInspectableButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AFInspectableButton.swift; sourceTree = "<group>"; };
65B7EA75197082AA0097E598 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
65D0169D19771DE2005220FB /* AFViewEffects.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AFViewEffects.swift; sourceTree = "<group>"; };
65D016A319771FD1005220FB /* AFViewHelper.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = AFViewHelper.podspec; sourceTree = "<group>"; };
Expand Down Expand Up @@ -99,6 +101,7 @@
65D0169C19771DE2005220FB /* AFViewHelper */ = {
isa = PBXGroup;
children = (
658A3B151D42BF210029B4E4 /* AFInspectableButton.swift */,
6535A0481BEBBE4400ECDA9D /* AFViewControllerAutoLayout.swift */,
6535A0461BEBBB2400ECDA9D /* AFViewAutoLayout.swift */,
65D0169D19771DE2005220FB /* AFViewEffects.swift */,
Expand Down Expand Up @@ -182,6 +185,7 @@
buildActionMask = 2147483647;
files = (
6535A0471BEBBB2400ECDA9D /* AFViewAutoLayout.swift in Sources */,
658A3B161D42BF210029B4E4 /* AFInspectableButton.swift in Sources */,
65889B731965051C0049740E /* ViewController.swift in Sources */,
65889B711965051C0049740E /* AppDelegate.swift in Sources */,
6535A0491BEBBE4400ECDA9D /* AFViewControllerAutoLayout.swift in Sources */,
Expand Down

0 comments on commit d076e4b

Please sign in to comment.