-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspinner.coffee
157 lines (118 loc) · 3.19 KB
/
spinner.coffee
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
class exports.spinnerView extends Layer
viewArray: []
circleArray: []
backgroundArray: []
isSpinning = false
constructor: (@options={}) ->
@options.duration ?= 3
@options.dotSize ?= 40
@options.dotCount ?= 4
@options.loaderHeight ?= 160
@options.dotColor ?= "#fff"
@options.width = 0
@options.height = 0
@options.opacity = 0
@options.hasBackgroundColor ?= ""
@options.backgroundOpacity ?= 1
super @options
@.centerX()
@.centerY()
@.y = @.y - 20
@.x = @.x - 20
@.states.add
hide:
opacity: 0
show:
opacity: 1
@.states.animationOptions =
time: .44
if @options.hasBackgroundColor isnt ""
immersiveBG = new Layer
width: Screen.width
height: Screen.height
backgroundColor: @options.hasBackgroundColor
opacity: @options.backgroundOpacity
immersiveBG.sendToBack()
immersiveBG.states.add
hide:
opacity: 0
show:
opacity: @options.backgroundOpacity
immersiveBG.states.animationOptions =
time: .44
immersiveBG.states.switchInstant "hide"
@backgroundArray.push(immersiveBG)
containerAnimation = new Animation
layer: @
properties:
rotation: @.rotation + 360
curve: "linear"
repeat: 1000
time: @options.duration * 0.932
# resets the animation on Stop
@.on Events.StateDidSwitch, (from, to, states) ->
if to is "hide"
for i in [[email protected]]
@viewArray[i].destroy()
@circleArray[i].destroy()
@circleArray = []
@viewArray = []
# Reset
@.rotation = 0
containerAnimation.stop()
@.on Events.StateWillSwitch, (from, to, states) ->
if to is "show"
containerAnimation.start()
circleAnimation: ->
for i in [[email protected]]
offset = Utils.round(i * 0.1 + 0.05, 2)
@viewArray[i].animate
properties:
rotation: @viewArray[i].rotation + 180
delay: offset
time: @options.duration / 4 + 0.3
curve: "ease-in-out"
scaleAnimationA = new Animation
layer: @circleArray[i]
properties:
scale: 0.44
delay: offset / 2
time: @options.duration / 8 + 0.15
curve: "ease-in-out"
scaleAnimationB = scaleAnimationA.reverse()
scaleAnimationA.on(Events.AnimationEnd, scaleAnimationB.start)
scaleAnimationA.start()
start: ->
return if isSpinning is true
isSpinning = true
if @options.hasBackgroundColor isnt ""
@backgroundArray[0].states.switch "show"
for i in [[email protected]]
v = new Layer
height: @options.dotSize
width: @options.dotSize
x: [email protected]/2
y: [email protected]/2
backgroundColor: "transparent"
@viewArray.push(v)
@.addChild(v)
c = new Layer
height: @options.dotSize
width: @options.dotSize
backgroundColor: @options.dotColor
y: [email protected] / 2
borderRadius: @options.dotSize / 2
@circleArray.push(c)
v.addChild(c)
@circleAnimation()
@.states.switch "show"
self = @
# Check when the last animation ended and start again
@viewArray[@options.dotCount-1].on Events.AnimationEnd, ->
self.circleAnimation()
stop: ->
return if isSpinning is false
isSpinning = false
@.states.switch "hide"
if @options.hasBackgroundColor isnt ""
@backgroundArray[0].states.switch "hide"