Last active
September 3, 2019 03:49
-
-
Save pgpt10/6bd876e3c8e43aba2843a52126d2954c to your computer and use it in GitHub Desktop.
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
@IBDesignable class DesignableView: UIView | |
{ | |
@IBInspectable var gradientColor1: UIColor = UIColor.white { | |
didSet{ | |
self.setGradient() | |
} | |
} | |
@IBInspectable var gradientColor2: UIColor = UIColor.white { | |
didSet{ | |
self.setGradient() | |
} | |
} | |
@IBInspectable var gradientStartPoint: CGPoint = .zero { | |
didSet{ | |
self.setGradient() | |
} | |
} | |
@IBInspectable var gradientEndPoint: CGPoint = CGPoint(x: 0, y: 1) { | |
didSet{ | |
self.setGradient() | |
} | |
} | |
private func setGradient() | |
{ | |
let gradientLayer = CAGradientLayer() | |
gradientLayer.colors = [self.gradientColor1.cgColor, self.gradientColor2.cgColor] | |
gradientLayer.startPoint = self.gradientStartPoint | |
gradientLayer.endPoint = self.gradientEndPoint | |
gradientLayer.frame = self.bounds | |
if let topLayer = self.layer.sublayers?.first, topLayer is CAGradientLayer | |
{ | |
topLayer.removeFromSuperlayer() | |
} | |
self.layer.addSublayer(gradientLayer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment