forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationController.swift
190 lines (167 loc) · 6.31 KB
/
AnimationController.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// RangeAnimatedContainer.swift
// GraphTest
//
// Created by Andrei Salavei on 3/12/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
protocol Animatable {
static func valueBetween(start: Self, end: Self, offset: Double) -> Self
}
enum TimeFunction {
case linear
case easeOut
case easeIn
case easeInOut
func profress(time: TimeInterval, duration: TimeInterval) -> TimeInterval {
switch self {
case .linear:
return time / duration
case .easeIn:
return (pow(2, 10 * (time / duration - 1)) - 0.0009765625) * 1.0009775171065499
case .easeOut:
return (-pow(2, -10 * time / duration)) + 1 * 1.0009775171065499
case .easeInOut:
let x = time / duration
if x < 1 / 2 {
return 4 * x * x * x
} else {
let f = 2 * x - 2
return 1 / 2 * f * f * f + 1
}
}
}
}
class AnimationController<AnimatableObject: Animatable> {
private(set) var isAnimating: Bool = false
private(set) var animationDuration: TimeInterval = 0.0
private(set) var currentTime: TimeInterval = 0.0
private(set) var start: AnimatableObject
private(set) var end: AnimatableObject
private(set) var current: AnimatableObject
var timeFunction: TimeFunction = .easeInOut
var refreshClosure: (() -> Void)?
// var updateClosure: ((AnimatableObject) -> Void)?
var completionClosure: (() -> Void)?
init(current: AnimatableObject, refreshClosure: (() -> Void)?) {
self.current = current
self.start = current
self.end = current
self.refreshClosure = refreshClosure
}
func animate(to: AnimatableObject, duration: TimeInterval, timeFunction: TimeFunction = .easeInOut) {
self.timeFunction = timeFunction
currentTime = 0
animationDuration = duration
if animationDuration > 0 {
start = current
end = to
isAnimating = true
DisplayLinkService.shared.add(listner: self)
} else {
start = to
end = to
current = to
isAnimating = false
DisplayLinkService.shared.remove(listner: self)
}
refreshClosure?()
}
func set(current: AnimatableObject) {
self.start = current
self.end = current
self.current = current
animationDuration = 0.0
currentTime = 0.0
// updateClosure?(current)
refreshClosure?()
if isAnimating {
isAnimating = false
DisplayLinkService.shared.remove(listner: self)
}
}
}
extension AnimationController: DisplayLinkListner {
func update(delta: TimeInterval) {
guard isAnimating else {
DisplayLinkService.shared.remove(listner: self)
return
}
currentTime += delta
if currentTime > animationDuration || animationDuration <= 0 {
start = end
current = end
isAnimating = false
animationDuration = 0.0
currentTime = 0.0
// updateClosure?(end)
completionClosure?()
refreshClosure?()
DisplayLinkService.shared.remove(listner: self)
} else {
let offset = timeFunction.profress(time: currentTime, duration: animationDuration)
current = AnimatableObject.valueBetween(start: start, end: end, offset: offset)
// updateClosure?(current)
refreshClosure?()
}
}
}
extension ClosedRange: Animatable where Bound: BinaryFloatingPoint {
static func valueBetween(start: ClosedRange<Bound>, end: ClosedRange<Bound>, offset: Double) -> ClosedRange<Bound> {
let castedOffset = Bound(offset)
return ClosedRange(uncheckedBounds: (lower: start.lowerBound + (end.lowerBound - start.lowerBound) * castedOffset,
upper: start.upperBound + (end.upperBound - start.upperBound) * castedOffset))
}
}
extension CGFloat: Animatable {
static func valueBetween(start: CGFloat, end: CGFloat, offset: Double) -> CGFloat {
return start + (end - start) * CGFloat(offset)
}
}
extension Double: Animatable {
static func valueBetween(start: Double, end: Double, offset: Double) -> Double {
return start + (end - start) * Double(offset)
}
}
extension Int: Animatable {
static func valueBetween(start: Int, end: Int, offset: Double) -> Int {
return start + Int(Double(end - start) * offset)
}
}
extension CGPoint: Animatable {
static func valueBetween(start: CGPoint, end: CGPoint, offset: Double) -> CGPoint {
return CGPoint(x: start.x + (end.x - start.x) * CGFloat(offset),
y: start.y + (end.y - start.y) * CGFloat(offset))
}
}
extension CGRect: Animatable {
static func valueBetween(start: CGRect, end: CGRect, offset: Double) -> CGRect {
return CGRect(x: start.origin.x + (end.origin.x - start.origin.x) * CGFloat(offset),
y: start.origin.y + (end.origin.y - start.origin.y) * CGFloat(offset),
width: start.width + (end.width - start.width) * CGFloat(offset),
height: start.height + (end.height - start.height) * CGFloat(offset))
}
}
struct NSColorContainer: Animatable {
var color: GColor
static func valueBetween(start: NSColorContainer, end: NSColorContainer, offset: Double) -> NSColorContainer {
return NSColorContainer(color: GColor.valueBetween(start: start.color, end: end.color, offset: offset))
}
}
extension GColor {
static func valueBetween(start: GColor, end: GColor, offset: Double) -> GColor {
let offsetF = CGFloat(offset)
let startCIColor = makeCIColor(color: start)
let endCIColor = makeCIColor(color: end)
return GColor(red: startCIColor.red + (endCIColor.red - startCIColor.red) * offsetF,
green: startCIColor.green + (endCIColor.green - startCIColor.green) * offsetF,
blue: startCIColor.blue + (endCIColor.blue - startCIColor.blue) * offsetF,
alpha: startCIColor.alpha + (endCIColor.alpha - startCIColor.alpha) * offsetF)
}
}