Files
Attractor/App/Common UI/GradientView.swift
James Magahern 5e0f1d252a ~buzzert/rossler.attix#8: Toolbar background does not change color when system changes dark mode
Redraw gradient views whenever trait collection changes
2020-10-29 15:33:46 -07:00

81 lines
2.4 KiB
Swift

//
// GradientView.swift
// App
//
// Created by James Magahern on 8/14/20.
//
import UIKit
class GradientView: UIImageView
{
enum Direction {
case horizontal
case vertical
}
var direction: Direction = .horizontal {
didSet { image = nil; setNeedsLayout() }
}
var colors: [UIColor] = [] {
didSet { image = nil; setNeedsLayout() }
}
private var generatedImageSize: CGSize?
convenience init(direction: Direction, colors: [UIColor]) {
self.init(image: nil)
self.direction = direction
self.colors = colors
}
private func gradientImage(forSize size: CGSize) -> UIImage? {
var image: UIImage? = nil
UIGraphicsBeginImageContext(size)
if let context = UIGraphicsGetCurrentContext() {
let gradientColorsArray = self.colors.map { $0.cgColor }
if let gradient = CGGradient(colorsSpace: nil, colors: gradientColorsArray as CFArray, locations: nil) {
if direction == .horizontal {
context.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: size.width, y: 0.0), options: CGGradientDrawingOptions())
} else if direction == .vertical {
context.drawLinearGradient(gradient, start: CGPoint(x: size.width / 2, y: 0), end: CGPoint(x: size.width / 2, y: size.height), options: CGGradientDrawingOptions())
}
}
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
}
return image
}
override func layoutSubviews() {
super.layoutSubviews()
var needsNewImage: Bool = image == nil
if let generatedImageSize = generatedImageSize {
if generatedImageSize != bounds.size {
needsNewImage = true
}
} else {
needsNewImage = true
}
if needsNewImage {
image = gradientImage(forSize: bounds.size)
generatedImageSize = bounds.size
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
// Regenerate image
image = nil
setNeedsLayout()
}
}