Created
January 21, 2019 10:23
-
-
Save milankamilya/4c28f379271c52743a33db9d1530eba7 to your computer and use it in GitHub Desktop.
GradientView manageable from Storyboard , iOS , IBDesignable
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 | |
/// CREDIT: https://stackoverflow.com/a/40957338/2666902 | |
/// GradientView is an IBDesignable Class. It will help you to put two gradient in a View | |
/// Even you can set whether the gradient will be put horizontally or vertically. | |
/// Instructions to use: | |
/// 1. set Class Name of the UIView to `GradientView` in storyboard (identity inspector) | |
/// 2. set specific colors/properties in attribute inspector | |
/// Now, you ready with Gradient. Run and See 👍 | |
/// | |
@IBDesignable | |
final class GradientView: UIView { | |
@IBInspectable var startColor: UIColor = UIColor.clear | |
@IBInspectable var endColor: UIColor = UIColor.clear | |
@IBInspectable var isHorizontalGradient: Bool = true | |
var gradient: CAGradientLayer? | |
override func draw(_ rect: CGRect) { | |
if let gradient = gradient { | |
gradient.removeFromSuperlayer() | |
} | |
gradient = CAGradientLayer() | |
gradient?.frame = bounds | |
gradient?.colors = [startColor.cgColor, endColor.cgColor] | |
gradient?.zPosition = -1 | |
// To know more about various gradient pattern checkout CAGradientLayer's doc | |
if isHorizontalGradient { | |
gradient?.startPoint = CGPoint(x: 0, y: 0.5) | |
gradient?.endPoint = CGPoint(x: 1, y: 0.5) | |
} | |
if let gradient = gradient { | |
layer.addSublayer(gradient) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment