91 lines
2.3 KiB
Swift
91 lines
2.3 KiB
Swift
//
|
|
// GradientView.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Zhandos Bolatbekov on 25.03.2021.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
struct GradientStop {
|
|
var color: UIColor
|
|
var location: Double
|
|
}
|
|
|
|
public class GradientView: UIView {
|
|
|
|
var stops: [GradientStop] = [] {
|
|
didSet {
|
|
self.updateColors()
|
|
self.updateLocations()
|
|
}
|
|
}
|
|
|
|
var horizontalMode: Bool = false { didSet { self.updatePoints() } }
|
|
var diagonalMode: Bool = false { didSet { self.updatePoints() } }
|
|
|
|
public var cornerRadius: CGFloat = 0 {
|
|
didSet {
|
|
self.layer.cornerRadius = self.cornerRadius
|
|
}
|
|
}
|
|
|
|
public var shadowColor: UIColor = .clear {
|
|
didSet {
|
|
self.layer.shadowColor = self.shadowColor.cgColor
|
|
}
|
|
}
|
|
|
|
public var shadowRadius: CGFloat = 0 {
|
|
didSet {
|
|
self.layer.shadowRadius = self.shadowRadius
|
|
}
|
|
}
|
|
|
|
public var shadowOpacity: CGFloat = 0 {
|
|
didSet {
|
|
self.layer.shadowOpacity = Float(self.shadowOpacity)
|
|
}
|
|
}
|
|
|
|
public var shadowOffsetY: CGFloat = 0 {
|
|
didSet {
|
|
self.layer.shadowOffset.height = self.shadowOffsetY
|
|
}
|
|
}
|
|
|
|
override public class var layerClass: AnyClass {
|
|
return CAGradientLayer.self
|
|
}
|
|
|
|
var gradientLayer: CAGradientLayer? {
|
|
return self.layer as? CAGradientLayer
|
|
}
|
|
|
|
func updatePoints() {
|
|
if self.horizontalMode {
|
|
self.gradientLayer?.startPoint = self.diagonalMode ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 0.5)
|
|
self.gradientLayer?.endPoint = self.diagonalMode ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0.5)
|
|
} else {
|
|
self.gradientLayer?.startPoint = self.diagonalMode ? CGPoint(x: 0, y: 0) : CGPoint(x: 0.5, y: 0)
|
|
self.gradientLayer?.endPoint = self.diagonalMode ? CGPoint(x: 1, y: 1) : CGPoint(x: 0.5, y: 1)
|
|
}
|
|
}
|
|
|
|
func updateLocations() {
|
|
self.gradientLayer?.locations = self.stops.map { $0.location as NSNumber }
|
|
}
|
|
|
|
func updateColors() {
|
|
self.gradientLayer?.colors = self.stops.map { $0.color.cgColor }
|
|
}
|
|
|
|
override public func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
updatePoints()
|
|
updateLocations()
|
|
updateColors()
|
|
}
|
|
}
|