-
Notifications
You must be signed in to change notification settings - Fork 184
/
LiquidLineEffect.swift
74 lines (64 loc) · 2.4 KB
/
LiquidLineEffect.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
//
// LiquidLineLoader.swift
// LiquidLoading
//
// Created by Takuma Yoshida on 2015/08/21.
// Copyright (c) 2015年 yoavlt. All rights reserved.
//
import Foundation
import UIKit
class LiquidLineEffect : LiquidLoadEffect {
var circleInter: CGFloat!
override func setupShape() -> [LiquittableCircle] {
return Array(0..<numberOfCircles).map { i in
return LiquittableCircle(
center: CGPoint(x: self.circleInter + self.circleRadius + CGFloat(i) * (self.circleInter + 2 * self.circleRadius), y: self.loader.frame.height * 0.5),
radius: self.circleRadius,
color: self.color,
growColor: self.growColor
)
}
}
override func movePosition(_ key: CGFloat) -> CGPoint {
if loader != nil {
return CGPoint(
x: (circles.last!.frame.rightBottom.x + circleInter) * sineTransform(key),
y: loader.frame.height * 0.5
)
} else {
return CGPoint.zero
}
}
func sineTransform(_ key: CGFloat) -> CGFloat {
return sin(key * CGFloat(Double.pi)) * 0.5 + 0.5
}
override func update() {
switch key {
case 0.0...2.0:
key += 2.0/(duration*60)
default:
key = 0.0
}
}
override func willSetup() {
if circleRadius == nil {
circleRadius = loader.frame.width * 0.05
}
self.circleInter = (loader.frame.width - 2 * circleRadius * CGFloat(numberOfCircles)) / CGFloat(numberOfCircles + 1)
self.engine = SimpleCircleLiquidEngine(radiusThresh: self.circleRadius, angleThresh: 0.2)
let moveCircleRadius = circleRadius * moveScale
self.moveCircle = LiquittableCircle(center: CGPoint(x: 0, y: loader.frame.height * 0.5), radius: moveCircleRadius, color: color, growColor: growColor)
}
override func resize() {
circles.map { circle in
return (circle, circle.center.minus(self.moveCircle!.center).length())
}.each { (circle, distance) in
let normalized = 1.0 - distance / (self.circleRadius + self.circleInter)
if normalized > 0.0 {
circle.radius = self.circleRadius + (self.circleRadius * self.circleScale - self.circleRadius) * normalized
} else {
circle.radius = self.circleRadius
}
}
}
}