-
Notifications
You must be signed in to change notification settings - Fork 184
/
LiquidLoadEffect.swift
133 lines (108 loc) · 3.18 KB
/
LiquidLoadEffect.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
//
// LiquidLoader.swift
// LiquidLoading
//
// Created by Takuma Yoshida on 2015/08/17.
// Copyright (c) 2015年 yoavlt. All rights reserved.
//
import Foundation
import UIKit
class LiquidLoadEffect : NSObject {
var numberOfCircles: Int
var duration: CGFloat
var circleScale: CGFloat = 1.17
var moveScale: CGFloat = 0.80
var color = UIColor.whiteColor()
var growColor = UIColor.redColor()
var engine: SimpleCircleLiquidEngine?
var moveCircle: LiquittableCircle?
var shadowCircle: LiquittableCircle?
var timer: CADisplayLink?
weak var loader: LiquidLoader!
var isGrow = false {
didSet {
grow(self.isGrow)
}
}
/* the following properties is initialized when frame is assigned */
var circles: [LiquittableCircle]!
var circleRadius: CGFloat!
var key: CGFloat = 0.0 {
didSet {
updateKeyframe(self.key)
}
}
init(loader: LiquidLoader, color: UIColor, circleCount: Int, duration: CGFloat, growColor: UIColor? = UIColor.redColor()) {
self.numberOfCircles = circleCount
self.duration = duration
self.circleRadius = loader.frame.width * 0.05
self.loader = loader
self.color = color
if growColor != nil {
self.growColor = growColor!
}
super.init()
setup()
}
func resize() {
// abstract
}
func setup() {
willSetup()
engine?.color = color
self.circles = setupShape()
for circle in circles {
loader?.addSubview(circle)
}
if moveCircle != nil {
loader?.addSubview(moveCircle!)
}
resize()
timer = CADisplayLink(target: self, selector: "update")
timer?.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
}
func updateKeyframe(key: CGFloat) {
self.engine?.clear()
let movePos = movePosition(key)
// move subviews positions
moveCircle?.center = movePos
shadowCircle?.center = movePos
circles.each { circle in
if self.moveCircle != nil {
self.engine?.push(self.moveCircle!, other: circle)
}
}
resize()
// draw and show grow
if let parent = loader {
self.engine?.draw(parent)
}
if let shadow = shadowCircle {
loader?.bringSubviewToFront(shadow)
}
}
func setupShape() -> [LiquittableCircle] {
return [] // abstract
}
func movePosition(key: CGFloat) -> CGPoint {
return CGPointZero // abstract
}
func update() {
// abstract
}
func willSetup() {
// abstract
}
func grow(isGrow: Bool) {
if isGrow {
shadowCircle = LiquittableCircle(center: self.moveCircle!.center, radius: self.moveCircle!.radius * 1.0, color: self.color, growColor: growColor)
shadowCircle?.isGrow = isGrow
loader?.addSubview(shadowCircle!)
} else {
shadowCircle?.removeFromSuperview()
}
}
func stopTimer() {
timer?.invalidate()
}
}