Nicer toolbar buttons

This commit is contained in:
James Magahern
2020-08-14 15:55:08 -07:00
parent 9b36a3ace5
commit 69029a3195
12 changed files with 534 additions and 187 deletions

View File

@@ -0,0 +1,72 @@
//
// 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
}
}
}