Skip to content

Commit 41966f8

Browse files
committed
Added gradient UIView
1 parent 5c6cc6b commit 41966f8

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

DJSwiftHelpers.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
62754BA725E691E200F2F489 /* Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62754BA525E691E200F2F489 /* Codable.swift */; };
6969
62754BA825E691E200F2F489 /* Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62754BA525E691E200F2F489 /* Codable.swift */; };
7070
627631A7255D512F005C09B1 /* DJSwiftHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 627631A6255D512F005C09B1 /* DJSwiftHelpers.swift */; };
71+
62BFAA94273ECDE600DAC879 /* Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62BFAA93273ECDE600DAC879 /* Gradient.swift */; };
7172
62C4485225A2208F0014F44B /* UIFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C4485125A2208F0014F44B /* UIFont.swift */; };
7273
62C4485325A2208F0014F44B /* UIFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C4485125A2208F0014F44B /* UIFont.swift */; };
7374
62C4485425A2208F0014F44B /* UIFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C4485125A2208F0014F44B /* UIFont.swift */; };
@@ -124,6 +125,7 @@
124125
6267B87826A07017009A31B1 /* UIApplication.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIApplication.swift; sourceTree = "<group>"; };
125126
62754BA525E691E200F2F489 /* Codable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Codable.swift; sourceTree = "<group>"; };
126127
627631A6255D512F005C09B1 /* DJSwiftHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DJSwiftHelpers.swift; sourceTree = "<group>"; };
128+
62BFAA93273ECDE600DAC879 /* Gradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Gradient.swift; sourceTree = "<group>"; };
127129
62C4485125A2208F0014F44B /* UIFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIFont.swift; sourceTree = "<group>"; };
128130
62D6CDE8255BFCCB00ABD216 /* Decodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Decodable.swift; sourceTree = "<group>"; };
129131
62D6CDEB255BFDA900ABD216 /* Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = "<group>"; };
@@ -212,6 +214,7 @@
212214
625380812594CE9B00C58B2F /* Double.swift */,
213215
627631A6255D512F005C09B1 /* DJSwiftHelpers.swift */,
214216
62DEDA482636D40A00771544 /* FileManager.swift */,
217+
62BFAA93273ECDE600DAC879 /* Gradient.swift */,
215218
62EC880426F8DACE00211A6A /* Locale.swift */,
216219
620F6A34250BB78200B10032 /* String.swift */,
217220
620F6A36250BB78200B10032 /* UIColor.swift */,
@@ -442,6 +445,7 @@
442445
6267B87C26A07017009A31B1 /* UIApplication.swift in Sources */,
443446
620F6A39250BB78200B10032 /* String.swift in Sources */,
444447
62D6CDF2255C002B00ABD216 /* URLRequest.swift in Sources */,
448+
62BFAA94273ECDE600DAC879 /* Gradient.swift in Sources */,
445449
620F6A48250BBC2F00B10032 /* UIDevice.swift in Sources */,
446450
6206C951256D6FF8003F3A67 /* URLSession.swift in Sources */,
447451
620F6A37250BB78200B10032 /* Array.swift in Sources */,

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ Parse a `json` file into the specified object type
149149
readJSONFromFile<T: Decodable>(fileURL: URL, type: T.Type) -> T?
150150
```
151151

152+
### Gradient
153+
154+
A `UIView` subclass with a gradient background. Can be used in a storyboard as it supports `IBDesignable`.
155+
152156
### Locale
153157

154158
Return the emoji flag for the given Locale

Sources/Common/Gradient.swift

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Gradient.swift
3+
// DJSwiftHelpers
4+
//
5+
// Created by Darren Jones on 12/11/2021.
6+
// Copyright © 2021 Dappological Ltd. All rights reserved.
7+
//
8+
9+
#if os(iOS)
10+
import UIKit
11+
12+
/**
13+
A `UIView` subclass with a gradient background.
14+
Can be used in Storyboards too.
15+
*/
16+
@IBDesignable
17+
public class Gradient: UIView {
18+
@IBInspectable var startColor: UIColor = .black { didSet { updateColors() }}
19+
@IBInspectable var endColor: UIColor = .white { didSet { updateColors() }}
20+
@IBInspectable var startLocation: Double = 0.05 { didSet { updateLocations() }}
21+
@IBInspectable var endLocation: Double = 0.95 { didSet { updateLocations() }}
22+
@IBInspectable var horizontalMode: Bool = false { didSet { updatePoints() }}
23+
@IBInspectable var diagonalMode: Bool = false { didSet { updatePoints() }}
24+
25+
override public class var layerClass: AnyClass { CAGradientLayer.self }
26+
27+
var gradientLayer: CAGradientLayer { layer as! CAGradientLayer }
28+
29+
func updatePoints() {
30+
if horizontalMode {
31+
gradientLayer.startPoint = diagonalMode ? .init(x: 1, y: 0) : .init(x: 0, y: 0.5)
32+
gradientLayer.endPoint = diagonalMode ? .init(x: 0, y: 1) : .init(x: 1, y: 0.5)
33+
} else {
34+
gradientLayer.startPoint = diagonalMode ? .init(x: 0, y: 0) : .init(x: 0.5, y: 0)
35+
gradientLayer.endPoint = diagonalMode ? .init(x: 1, y: 1) : .init(x: 0.5, y: 1)
36+
}
37+
}
38+
func updateLocations() {
39+
gradientLayer.locations = [startLocation as NSNumber, endLocation as NSNumber]
40+
}
41+
func updateColors() {
42+
gradientLayer.colors = [startColor.cgColor, endColor.cgColor]
43+
}
44+
override public func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
45+
super.traitCollectionDidChange(previousTraitCollection)
46+
updatePoints()
47+
updateLocations()
48+
updateColors()
49+
}
50+
}
51+
#endif

0 commit comments

Comments
 (0)