forked from vinayjn/Spinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spinner.swift
executable file
·170 lines (133 loc) · 5.71 KB
/
Spinner.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Spinner.swift
// Search
//
// Created by Vinay Jain on 7/19/15.
// Copyright (c) 2015 Vinay Jain. All rights reserved.
//
import UIKit
@IBDesignable
class Spinner: UIView {
enum SpinnerStyle : Int{
case None = 0
case Light = 1
case Dark = 2
}
var Style : SpinnerStyle = .None
@IBInspectable var hidesWhenStopped : Bool = false
@IBInspectable var outerFillColor : UIColor = UIColor.clear
@IBInspectable var outerStrokeColor : UIColor = UIColor.clear
@IBInspectable var outerLineWidth : CGFloat = 5.0
@IBInspectable var outerEndStroke : CGFloat = 0.5
@IBInspectable var outerAnimationDuration : CGFloat = 2.0
@IBInspectable var enableInnerLayer : Bool = true
@IBInspectable var innerFillColor : UIColor = UIColor.clear
@IBInspectable var innerStrokeColor : UIColor = UIColor.gray
@IBInspectable var innerLineWidth : CGFloat = 5.0
@IBInspectable var innerEndStroke : CGFloat = 0.5
@IBInspectable var innerAnimationDuration : CGFloat = 1.6
@IBInspectable var labelText : String = ""
@IBInspectable var labelFont : String = "Helvetica"
@IBInspectable var labelTextColor : UIColor = UIColor.black
@IBInspectable var labelFontSize : CGFloat = 11.0
var currentInnerRotation : CGFloat = 0
var currentOuterRotation : CGFloat = 0
var innerView : UIView = UIView()
var outerView : UIView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit(){
self.backgroundColor = UIColor.clear
}
override func draw(_ rect: CGRect) {
switch Style{
case .Dark:
outerStrokeColor = UIColor.gray
innerStrokeColor = UIColor.gray
labelTextColor = UIColor.gray
case .Light:
outerStrokeColor = UIColor ( red: 0.9333, green: 0.9333, blue: 0.9333, alpha: 1.0 )
innerStrokeColor = UIColor ( red: 0.9333, green: 0.9333, blue: 0.9333, alpha: 1.0 )
labelTextColor = UIColor ( red: 0.9333, green: 0.9333, blue: 0.9333, alpha: 1.0 )
case .None:
break
}
self.addSubview(outerView)
outerView.frame = CGRect(x: 0 , y: 0, width: rect.size.width, height: rect.size.height)
outerView.center = self.convert(self.center, from: self.superview!)
let outerLayer = CAShapeLayer()
outerLayer.path = UIBezierPath(ovalIn: outerView.bounds).cgPath
outerLayer.lineWidth = outerLineWidth
outerLayer.strokeStart = 0.0
outerLayer.strokeEnd = outerEndStroke
outerLayer.lineCap = kCALineCapRound
outerLayer.fillColor = outerFillColor.cgColor
outerLayer.strokeColor = outerStrokeColor.cgColor
outerView.layer.addSublayer(outerLayer)
if enableInnerLayer{
self.addSubview(innerView)
innerView.frame = CGRect(x: 0, y: 0, width: rect.size.width - 20, height: rect.size.height - 20)
innerView.center = self.convert(self.center, from: self.superview!)
let innerLayer = CAShapeLayer()
innerLayer.path = UIBezierPath(ovalIn: innerView.bounds).cgPath
innerLayer.lineWidth = innerLineWidth
innerLayer.strokeStart = 0
innerLayer.strokeEnd = innerEndStroke
innerLayer.lineCap = kCALineCapRound
innerLayer.fillColor = innerFillColor.cgColor
innerLayer.strokeColor = innerStrokeColor.cgColor
innerView.layer.addSublayer(innerLayer)
}
let label = UILabel()
label.text = labelText
label.textColor = labelTextColor
label.font = UIFont(name: labelFont, size: labelFontSize)
self.addSubview(label)
if enableInnerLayer{
label.frame.size.width = innerView.frame.size.width/1.20
label.frame.size.height = innerView.frame.size.height
} else {
label.frame.size.width = outerView.frame.size.width/1.2
label.frame.size.height = outerView.frame.size.height
}
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.textAlignment = .center
label.center = self.convert(self.center, from: self.superview!)
self.startAnimating()
}
func animateInnerRing(){
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.fromValue = 0 * CGFloat(M_PI/180)
rotationAnimation.toValue = 360 * CGFloat(M_PI/180)
rotationAnimation.duration = Double(innerAnimationDuration)
rotationAnimation.repeatCount = HUGE
self.innerView.layer.add(rotationAnimation, forKey: "rotateInner")
}
func animateOuterRing(){
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.fromValue = 360 * CGFloat(M_PI/180)
rotationAnimation.toValue = 0 * CGFloat(M_PI/180)
rotationAnimation.duration = Double(outerAnimationDuration)
rotationAnimation.repeatCount = HUGE
self.outerView.layer.add(rotationAnimation, forKey: "rotateOuter")
}
func startAnimating(){
self.isHidden = false
self.animateOuterRing()
self.animateInnerRing()
}
func stopAnimating(){
if hidesWhenStopped{
self.isHidden = true
}
self.outerView.layer.removeAllAnimations()
self.innerView.layer.removeAllAnimations()
}
}