Created
February 27, 2017 23:24
-
-
Save ozgurshn/f4fb762d7b64e410390ca2c772fcf0ae to your computer and use it in GitHub Desktop.
IBInspectable for Rounded and Gradient UIView
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
import UIKit | |
@IBDesignable | |
class CustomView: UIView { | |
var fromColor: UIColor? | |
var toColor: UIColor? | |
var gradientLayer: CAGradientLayer? | |
@IBInspectable var cornerRadius: CGFloat = 0 { | |
didSet { | |
layer.cornerRadius = cornerRadius | |
layer.masksToBounds = cornerRadius > 0 | |
} | |
} | |
@IBInspectable var topColor: UIColor? { | |
didSet { | |
fromColor = topColor | |
setGradient() | |
} | |
} | |
@IBInspectable var bottomColor: UIColor? { | |
didSet { | |
toColor = bottomColor | |
setGradient() | |
} | |
} | |
func setGradient() { | |
let gradientLocations = [0.0,1.0] | |
let gradientLayer = CAGradientLayer() | |
if fromColor != nil && toColor != nil | |
{ | |
gradientLayer.colors = [fromColor!.cgColor, toColor!.cgColor] | |
} | |
gradientLayer.locations = gradientLocations as [NSNumber] | |
gradientLayer.frame = self.bounds | |
self.layer.insertSublayer(gradientLayer, at: 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment