SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2.  
  3. @IBDesignable
  4. open class GradientView: UIView {
  5.    
  6.     @IBInspectable
  7.     open var beginColor: UIColor = .white {
  8.         didSet {
  9.             updateGradient()
  10.         }
  11.     }
  12.    
  13.     @IBInspectable
  14.     open var endColor: UIColor = .red {
  15.         didSet {
  16.             updateGradient()
  17.         }
  18.     }
  19.    
  20.     @IBInspectable
  21.     open var startPoint: CGPoint = CGPoint(x: 1, y: 0) {
  22.         didSet {
  23.             updateGradient()
  24.         }
  25.     }
  26.    
  27.     @IBInspectable
  28.     open var endPoint: CGPoint = CGPoint(x: 0, y: 1) {
  29.         didSet {
  30.             updateGradient()
  31.         }
  32.     }
  33.    
  34.     private var gradientLayer: CAGradientLayer!
  35.    
  36.     override init(frame: CGRect) {
  37.         super.init(frame: frame)
  38.         configure()
  39.         updateGradient()
  40.     }
  41.    
  42.     required public init?(coder aDecoder: NSCoder) {
  43.         super.init(coder: aDecoder)
  44.         configure()
  45.         updateGradient()
  46.     }
  47.    
  48.     private func configure() {
  49.         gradientLayer = CAGradientLayer()
  50.         layer.insertSublayer(gradientLayer, at: 0)
  51.     }
  52.    
  53.     private func updateGradient() {
  54.         gradientLayer.colors = [beginColor.cgColor, endColor.cgColor]
  55.         gradientLayer.locations = [0, 1]
  56.         gradientLayer.startPoint = startPoint
  57.         gradientLayer.endPoint = endPoint
  58.     }
  59.    
  60.     override open func layoutSubviews() {
  61.         super.layoutSubviews()
  62.         gradientLayer.frame = bounds
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top