-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSwiftyGradient.swift
72 lines (56 loc) · 1.65 KB
/
SwiftyGradient.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
//
// SwiftyGardient.swift
// SwiftyGardient
//
// Created by Bilal Karim Reffas on 09.11.15.
// Copyright © 2015 Bilal Karim Reffas. All rights reserved.
//
import UIKit
@IBDesignable class SwiftyGradient: UIView {
/// The startColor for the Gardient
@IBInspectable var startColor: UIColor = UIColor.whiteColor() {
didSet{
setupSwiftyGradient()
}
}
/// Thet endColor for the Gardient
@IBInspectable var endColor: UIColor = UIColor.blackColor() {
didSet{
setupSwiftyGradient()
}
}
///Draw the Gardient Horizontal
@IBInspectable var isHorizontal: Bool = false {
didSet{
setupSwiftyGradient()
}
}
///The roundness for the corner
@IBInspectable var cornerRadius: CGFloat = 0.0 {
didSet{
setupSwiftyGradient()
}
}
func setupSwiftyGradient() {
let colors = [startColor.CGColor, endColor.CGColor]
gradientLayer.colors = colors
gradientLayer.cornerRadius = cornerRadius
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = isHorizontal ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 1)
setNeedsDisplay()
}
var gradientLayer: CAGradientLayer {
return self.layer as! CAGradientLayer
}
override class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
override init(frame: CGRect) {
super.init(frame: frame)
setupSwiftyGradient()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupSwiftyGradient()
}
}