-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradientShadowView.swift
111 lines (93 loc) · 2.92 KB
/
GradientShadowView.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// GradientShadowView.swift
// GradientShadowView
//
// Created by Administrator on 3/13/19.
// Copyright © 2019 Administrator. All rights reserved.
//
import UIKit
@IBDesignable class GradientShadowView: UIView {
private var gradientLayer: CAGradientLayer!
@IBInspectable var topColor: UIColor = .red {
didSet {
setNeedsLayout()
}
}
@IBInspectable var bottomColor: UIColor = .yellow {
didSet {
setNeedsLayout()
}
}
@IBInspectable var shadowColor: UIColor = .clear {
didSet {
setNeedsLayout()
}
}
@IBInspectable var shadowX: CGFloat = 0 {
didSet {
setNeedsLayout()
}
}
@IBInspectable var shadowY: CGFloat = -3 {
didSet {
setNeedsLayout()
}
}
@IBInspectable var shadowBlur: CGFloat = 3 {
didSet {
setNeedsLayout()
}
}
@IBInspectable var startPointX: CGFloat = 0 {
didSet {
setNeedsLayout()
}
}
@IBInspectable var startPointY: CGFloat = 0.5 {
didSet {
setNeedsLayout()
}
}
@IBInspectable var endPointX: CGFloat = 1 {
didSet {
setNeedsLayout()
}
}
@IBInspectable var endPointY: CGFloat = 0.5 {
didSet {
setNeedsLayout()
}
}
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
setNeedsLayout()
}
}
override class var layerClass: AnyClass {
return CAGradientLayer.self
}
override func layoutSubviews() {
self.gradientLayer = self.layer as? CAGradientLayer
self.gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
self.gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY)
self.gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY)
self.layer.cornerRadius = cornerRadius
self.layer.shadowColor = shadowColor.cgColor
self.layer.shadowOffset = CGSize(width: shadowX, height: shadowY)
self.layer.shadowRadius = shadowBlur
self.layer.shadowOpacity = 1
}
func animate(duration: TimeInterval, newTopColor: UIColor, newBottomColor: UIColor) {
let fromColors = self.gradientLayer?.colors
let toColors: [AnyObject] = [ newTopColor.cgColor, newBottomColor.cgColor]
self.gradientLayer?.colors = toColors
let animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
animation.fromValue = fromColors
animation.toValue = toColors
animation.duration = duration
animation.isRemovedOnCompletion = true
animation.fillMode = .forwards
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
self.gradientLayer?.add(animation, forKey:"animateGradient")
}
}