-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathDynamicCounterTextView.swift
240 lines (210 loc) · 9.73 KB
/
DynamicCounterTextView.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// DynamicCounterTextView.swift
// Telegram
//
// Created by Mikhail Filimonov on 30/11/2020.
// Copyright © 2020 Telegram. All rights reserved.
//
import Foundation
import AppKit
public final class DynamicCounterTextView : View {
public struct Value {
public let values: [(TextViewLayout, DynamicCounterTextView.Text)]
public let size: NSSize
public let numSize: CGFloat
}
public static func make(for text: String, count: String, font: NSFont, textColor: NSColor, width: CGFloat, onlyFade: Bool = false) -> Value {
var title: [(String, DynamicCounterTextView.Text.Animation, Int)] = []
if count.isEmpty {
title = [(text, .crossFade, 0)]
} else {
var text = text
let range = text.nsstring.range(of: count)
if range.location != NSNotFound {
title.append((text.nsstring.substring(to: range.location), .crossFade, 0))
var index: Int = 0
for _ in range.lowerBound ..< range.upperBound {
let symbol = text.nsstring.substring(with: NSMakeRange(range.location + index, 1))
let animation: Text.Animation
if Int(symbol) != nil {
animation = onlyFade ? .crossFade : .numeric
} else {
animation = .crossFade
}
title.append((symbol, animation, index + 1))
index += 1
}
title.append((text.nsstring.substring(from: range.upperBound), .crossFade, range.length + 1))
} else {
title.append((text, .crossFade, 0))
text = ""
}
}
title = title.filter { !$0.0.isEmpty }
let texts:[DynamicCounterTextView.Text] = title.map {
return .init(text: .initialize(string: $0.0, color: textColor, font: font), animation: $0.1, index: $0.2)
}
let layouts = texts.map {
return (TextViewLayout($0.text, maximumNumberOfLines: 1, truncationType: .end, alignment: .center), $0)
}
let numeroSize: CGFloat = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0].map { value -> TextViewLayout in
let layout = TextViewLayout(.initialize(string: "\(value)", color: nil, font: font), maximumNumberOfLines: 1, truncationType: .end, alignment: .center)
layout.measure(width: .greatestFiniteMagnitude)
return layout
}.map {
$0.layoutSize.width
}.max()!
var mw: CGFloat = width
for layout in layouts {
layout.0.measure(width: mw)
var w = layout.0.layoutSize.width
if Int(layout.0.attributedString.string) != nil {
w = max(w, numeroSize)
}
mw -= max(w, 4) - 2
}
let size = layouts.reduce(NSZeroSize, { current, value in
var current = current
if value.0.attributedString.string == " " {
current.width += 4
} else {
var w = value.0.layoutSize.width
if Int(value.0.attributedString.string) != nil {
w = max(w, numeroSize)
}
current.width += w
}
current.height = max(current.height, value.0.layoutSize.height)
return current
})
return Value(values: layouts, size: size, numSize: numeroSize)
}
private let duration: TimeInterval = 0.4
private let timingFunction: CAMediaTimingFunctionName = .spring
public struct Text : Hashable, Comparable {
public enum Animation : Equatable {
case crossFade
case numeric
}
let text: NSAttributedString
let animation: Animation
let index: Int
public init(text: NSAttributedString, animation: Animation, index: Int) {
self.text = text
self.animation = animation
self.index = index
}
public static func <(lhs: Text, rhs: Text) -> Bool {
let lhsInt: Int? = Int(lhs.text.string)
let rhsInt: Int? = Int(rhs.text.string)
if let lhsInt = lhsInt, let rhsInt = rhsInt {
return lhsInt < rhsInt
}
return false
}
public func hash(into hasher: inout Hasher) {
hasher.combine(index)
}
}
public var effectiveSubviews: [TextView] {
return textViews.map { $0.value.0 }
}
fileprivate var textViews: [Text : (TextView, Text)] = [:]
fileprivate var layouts: [(TextViewLayout, Text)] = []
public func update(_ value: Value, animated: Bool, reversed: Bool = false) {
enum NumericAnimation {
case forward
case backward
}
let layouts = value.values
let texts = layouts.map { $0.1 }
let previous = Int(self.layouts.compactMap { Int($0.1.text.string) }.reduce("", { current, value in
return current + "\(value)"
})) ?? 0
let current = Int(layouts.compactMap { Int($0.1.text.string) }.reduce("", { current, value in
return current + "\(value)"
})) ?? 0
if self.layouts.map({ $0.1 }) == layouts.map({ $0.1 }) {
return
}
self.layouts = layouts
let numberAnimation: NumericAnimation = (reversed ? current > previous : current < previous) ? .backward : .forward
var addition: [Int : NumericAnimation] = [:]
var previousTextPos:[Int: NSPoint] = [:]
for (key, textView) in textViews {
let title = texts.first(where: { $0.hashValue == key.hashValue })
if textView.1 != title {
let updated = title ?? key
if let _ = title {
addition[key.hashValue] = numberAnimation//title < key ? .backward : .forward
}
textViews[key] = nil
let field = textView.0
previousTextPos[key.hashValue] = field.frame.origin
if animated {
switch updated.animation {
case .crossFade:
field.layer?.animateAlpha(from: 1, to: 0, duration: duration - 0.1, timingFunction: timingFunction, removeOnCompletion: false, completion: { [weak field] _ in
field?.removeFromSuperview()
})
case .numeric:
field.layer?.animateAlpha(from: 1, to: 0, duration: duration - 0.1, timingFunction: timingFunction, removeOnCompletion: false, completion: { [weak field] _ in
field?.removeFromSuperview()
})
let direction = addition[key.hashValue]
switch direction {
case .forward?:
field.layer?.animatePosition(from: field.frame.origin, to: NSMakePoint(field.frame.minX, field.frame.maxY), timingFunction: timingFunction, removeOnCompletion: false)
case .backward?:
field.layer?.animatePosition(from: field.frame.origin, to: NSMakePoint(field.frame.minX, field.frame.minY - field.frame.height), timingFunction: timingFunction, removeOnCompletion: false)
case .none:
break
}
}
} else {
field.removeFromSuperview()
}
}
}
var pos:NSPoint = .zero
for layout in layouts {
if let view = textViews[layout.1] {
if animated {
view.0.layer?.animatePosition(from: NSMakePoint(view.0.frame.origin.x - pos.x, view.0.frame.origin.y - pos.y), to: .zero, timingFunction: timingFunction, removeOnCompletion: true, additive: true)
}
view.0.setFrameOrigin(pos)
} else {
let current = TextView()
current.userInteractionEnabled = false
current.isSelectable = false
current.disableBackgroundDrawing = true
self.textViews[layout.1] = (current, layout.1)
current.update(layout.0, origin: pos)
addSubview(current)
if animated {
switch layout.1.animation {
case .crossFade:
current.layer?.animateAlpha(from: 0, to: 1, duration: duration)
case .numeric:
let prevPos = previousTextPos[layout.1.hashValue] ?? pos
let direction = addition[layout.1.hashValue]
switch direction {
case .forward?:
current.layer?.animatePosition(from: NSMakePoint(pos.x, pos.y - layout.0.layoutSize.height), to: pos, timingFunction: timingFunction)
case .backward?:
current.layer?.animatePosition(from: NSMakePoint(pos.x, pos.y + layout.0.layoutSize.height), to: pos, timingFunction: timingFunction)
case .none:
break
}
current.layer?.animateAlpha(from: 0, to: 1, duration: duration)
}
}
}
if layout.0.attributedString.string == " " {
pos.x += 4
} else {
pos.x += layout.0.layoutSize.width
}
}
}
}