Created
April 16, 2020 14:54
-
-
Save foxicode/5e1ccfcd43265085781cd8538d17662d to your computer and use it in GitHub Desktop.
iOS gradient view
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 GradientView: UIView { | |
| enum Direction: Int { | |
| case horizontal | |
| case vertical | |
| } | |
| @IBInspectable var startColor: UIColor = .white { | |
| didSet { | |
| gradientLayer?.colors = [ | |
| startColor.cgColor, | |
| endColor.cgColor | |
| ] | |
| } | |
| } | |
| @IBInspectable var endColor: UIColor = .black { | |
| didSet { | |
| gradientLayer?.colors = [ | |
| startColor.cgColor, | |
| endColor.cgColor | |
| ] | |
| } | |
| } | |
| @IBInspectable var direction: Int = Direction.horizontal.rawValue { | |
| didSet { | |
| if direction == Direction.horizontal.rawValue { | |
| gradientLayer?.startPoint = CGPoint(x: 0.0, y: 0.5) | |
| gradientLayer?.endPoint = CGPoint(x: 1.0, y: 0.5) | |
| } else { | |
| gradientLayer?.startPoint = CGPoint(x: 0.5, y: 0.0) | |
| gradientLayer?.endPoint = CGPoint(x: 0.5, y: 1.0) | |
| } | |
| } | |
| } | |
| var gradientLayer: CAGradientLayer? = nil | |
| override func layoutSubviews() { | |
| super.layoutSubviews() | |
| if gradientLayer == nil { | |
| gradientLayer = CAGradientLayer() | |
| gradientLayer!.colors = [ | |
| startColor.cgColor, | |
| endColor.cgColor | |
| ] | |
| if direction == Direction.horizontal.rawValue { | |
| gradientLayer!.startPoint = CGPoint(x: 0.0, y: 0.5) | |
| gradientLayer!.endPoint = CGPoint(x: 1.0, y: 0.5) | |
| } else { | |
| gradientLayer!.startPoint = CGPoint(x: 0.5, y: 0.0) | |
| gradientLayer!.endPoint = CGPoint(x: 0.5, y: 1.0) | |
| } | |
| layer.addSublayer(gradientLayer!) | |
| } | |
| gradientLayer?.cornerRadius = layer.cornerRadius | |
| gradientLayer?.frame = self.bounds | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment