-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBGradientView.swift
97 lines (86 loc) · 2.49 KB
/
RBGradientView.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// RBGradientView.swift
// Music
//
// Created by Bhavin on 10/12/16.
// Copyright © 2016 bhavin ramani. All rights reserved.
//
import Foundation
import UIKit
@IBDesignable
class GradientView: UIView {
@IBInspectable var startColor: UIColor = .white {
didSet {
layoutSubviews()
}
}
@IBInspectable var endColor: UIColor = .black{
didSet {
layoutSubviews()
}
}
@IBInspectable var startLocation: Double = 0.05 {
didSet {
layoutSubviews()
}
}
@IBInspectable var endLocation: Double = 0.95 {
didSet {
layoutSubviews()
}
}
@IBInspectable var horizontalMode: Bool = false {
didSet {
layoutSubviews()
}
}
@IBInspectable var diagonalMode: Bool = false {
didSet {
layoutSubviews()
}
}
// add border color, width and corner radius properties to your GradientView
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layoutSubviews()
}
}
@IBInspectable var borderColor: UIColor = .clear {
didSet {
layoutSubviews()
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layoutSubviews()
}
}
override class var layerClass: AnyClass { return CAGradientLayer.self }
override func layoutSubviews() {
super.layoutSubviews()
guard let layer = layer as? CAGradientLayer else { return }
if horizontalMode {
if diagonalMode {
layer.startPoint = CGPoint(x: 1, y: 0)
layer.endPoint = CGPoint(x: 0, y: 1)
} else {
layer.startPoint = CGPoint(x: 0, y: 0.5)
layer.endPoint = CGPoint(x: 1, y: 0.5)
}
} else {
if diagonalMode {
layer.startPoint = CGPoint(x: 0, y: 0)
layer.endPoint = CGPoint(x: 1, y: 1)
} else {
layer.startPoint = CGPoint(x: 0.5, y: 0)
layer.endPoint = CGPoint(x: 0.5, y: 1)
}
}
layer.locations = [startLocation as NSNumber, endLocation as NSNumber]
layer.colors = [startColor.cgColor, endColor.cgColor]
// add border and corner radius also to your layer
layer.cornerRadius = cornerRadius
layer.borderColor = borderColor.cgColor
layer.borderWidth = borderWidth
}
}