Last active
February 7, 2020 06:06
-
-
Save turk-jk/4ecf367296cc190dd795bd8cd340ef18 to your computer and use it in GitHub Desktop.
View that creates a border with gradient colours.
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
| // | |
| // YKGradientBorder.swift | |
| // YKGradientBorder | |
| // | |
| // Created by Yacob Kazal on 5/2/20. | |
| // Copyright © 2020 Litt Global. All rights reserved. | |
| // | |
| import UIKit | |
| open class YKGradientBorder: UIView { | |
| // private variables | |
| private var DefaultGradientBorderColors: [UIColor] = [ | |
| #colorLiteral(red: 0, green: 0.337254902, blue: 0.4941176471, alpha: 1), | |
| #colorLiteral(red: 0.2208813892, green: 0.6252568753, blue: 0.683234582, alpha: 1), | |
| #colorLiteral(red: 0, green: 0.337254902, blue: 0.4941176471, alpha: 1)// Repeat the first color to make a smooth transition | |
| ] | |
| private var DefaultGradientBorderWidth: CGFloat = 4 | |
| public var gradientBorderWidth: CGFloat { | |
| get{return DefaultGradientBorderWidth} | |
| } | |
| // change width of the border | |
| public func setWidth(newWidth:CGFloat) { | |
| setupGradientLayer( borderWidth: newWidth) | |
| } | |
| /// Change the colors | |
| public func setColors(newColors: [UIColor]) { | |
| setupGradientLayer(borderColors: newColors) | |
| } | |
| // Set the UIView's layer class to be our CAGradientLayer | |
| override open class var layerClass : AnyClass { | |
| return CAGradientLayer.self | |
| } | |
| // Initialiser | |
| required public init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| setupGradientLayer() | |
| } | |
| // to make sure the layer is fitted to the new layout | |
| open override func layoutSubviews() { | |
| super.layoutSubviews() | |
| setupGradientLayer() | |
| } | |
| // Custom initialiser | |
| public init(frame: CGRect, borderColors gradientBorderColors: [UIColor]? = nil, borderWidth gradientBorderWidth: CGFloat? = nil) { | |
| super.init(frame: frame) | |
| setupGradientLayer(borderColors: gradientBorderColors, borderWidth: gradientBorderWidth) | |
| } | |
| // Setup or change the border attribute | |
| private func setupGradientLayer(borderColors gradientBorderColors: [UIColor]? = nil, borderWidth gradientBorderWidth: CGFloat? = nil) { | |
| let width = gradientBorderWidth ?? DefaultGradientBorderWidth | |
| let colors = gradientBorderColors ?? DefaultGradientBorderColors | |
| DefaultGradientBorderWidth = width | |
| DefaultGradientBorderColors = colors | |
| // Grab this UIView's layer and cast it as CAGradientLayer | |
| let _layer: CAGradientLayer = self.layer as! CAGradientLayer | |
| // we need to scale it to match the display of the device. | |
| _layer.contentsScale = UIScreen.main.scale | |
| // Set the gradient colors | |
| _layer.colors = colors.map{$0.cgColor} | |
| if #available(iOS 12.0, *) { | |
| _layer.type = .conic | |
| } else { | |
| // Fallback on earlier versions | |
| } | |
| _layer.startPoint = CGPoint(x: 0.5, y: 0.5) | |
| _layer.endPoint = CGPoint(x: 0.5, y: 0) | |
| let rect = CGRect( | |
| x: width/4, | |
| y: width/4, | |
| width: _layer.bounds.width - width, | |
| height: _layer.bounds.height - width ) | |
| let shapeLayer = CAShapeLayer() | |
| shapeLayer.frame = rect | |
| shapeLayer.lineWidth = width | |
| shapeLayer.fillColor = nil | |
| shapeLayer.strokeColor = UIColor.white.cgColor | |
| let arcCenter = shapeLayer.position | |
| let radius = shapeLayer.bounds.size.width / 2.0 | |
| let startAngle = CGFloat(0.0) | |
| let endAngle = CGFloat(2.0 * .pi) | |
| let clockwise = true | |
| let circlePath = UIBezierPath(arcCenter: arcCenter, | |
| radius: radius, | |
| startAngle: startAngle, | |
| endAngle: endAngle, | |
| clockwise: clockwise) | |
| shapeLayer.path = circlePath.cgPath | |
| _layer.mask = shapeLayer | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
Public methods
You can specify the colours and the border width in the Initialiser. As well as, afterwards.
like so
for best performance is to set the border width and the colours in Initialiser
like so
Colours Array
The array needs to end with the same colour of the beginning.