-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradientView.swift
69 lines (46 loc) · 2.23 KB
/
GradientView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import UIKit
/// Designable view with linear gradient settings
@IBDesignable
class GradientView: UIView {
@IBInspectable var topGradientColor: UIColor!
@IBInspectable var bottomGradientColor: UIColor!
@IBInspectable var locationTop: CGFloat = 0.0
@IBInspectable var locationBottom: CGFloat = 1.0
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = UIColor.clear
}
override func draw(_ rect: CGRect) {
let colorSpace = CGColorSpaceCreateDeviceRGB();
let context = UIGraphicsGetCurrentContext();
let gradientColors = [topGradientColor.cgColor, bottomGradientColor.cgColor];
let gradientLocations: [CGFloat] = [locationTop, locationBottom]
let gradient = CGGradient(colorsSpace: colorSpace, colors: gradientColors as CFArray, locations: gradientLocations)
let startPoint = CGPoint(x: 0, y: 0)
let endPoint = CGPoint(x: 0, y: bounds.height)
context?.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: [])
}
}
/// Designable view with radial gradient settings
@IBDesignable
class RadialGradientView: UIView {
@IBInspectable var topGradientColor: UIColor! = UIColor.black
@IBInspectable var bottomGradientColor: UIColor! = UIColor.white
@IBInspectable var startRadius: CGFloat = 10
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = UIColor.clear
}
override func draw(_ rect: CGRect) {
let colorSpace = CGColorSpaceCreateDeviceRGB();
let context = UIGraphicsGetCurrentContext();
let gradientColors = [topGradientColor.cgColor, bottomGradientColor.cgColor];
let gradientLocations: [CGFloat] = [0.0, 1.0]
let gradient = CGGradient(colorsSpace: colorSpace, colors: gradientColors as CFArray, locations: gradientLocations)
var radius = frame.width
if radius < frame.height {
radius = frame.height
}
context?.drawRadialGradient(gradient!, startCenter: center, startRadius: startRadius, endCenter: center, endRadius: radius, options: [])
}
}