-
Notifications
You must be signed in to change notification settings - Fork 0
/
CirclePackView.swift
411 lines (364 loc) ยท 14.5 KB
/
CirclePackView.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
// CirclePackView.swift
// Keyme
//
// Created by Young Bin on 2023/07/25.
// Copyright ยฉ 2023 team.humanwave. All rights reserved.
//
import SwiftUI
import Core
import ComposableArchitecture
import Domain
import DSKit
import Foundation
public class CirclePackViewOption<DetailView: View> {
var activateCircleBlink: Bool
/// Circle pack ๊ทธ๋ํ ๋ฐฐ๊ฒฝ์
var background: Color
/// Circle pack ๊ทธ๋ํ์ ํฌ๊ธฐ
var outboundLength: CGFloat
/// Circle pack ๊ทธ๋ํ์ ํฌ๊ธฐ์ ์ค ํจ๋ฉ๊ฐ. ์ฆ, ๋ทฐ ์ ์ฒด ํ๋ ์๊ณผ ๊ทธ๋ํ ์ฌ์ด์ ๋ ๊ฑฐ๋ฆฌ.
var framePadding: CGFloat
/// Circle pack ๊ทธ๋ํ๊ฐ ํ๋๋์ ๋ ๊ทธ ์์ด ๊ฐ์ง ํ๋ฉด ๊ฐ๋ก๊ธธ์ด์ ๋ํ ๋น์จ์
๋๋ค.
var magnifiedCircleRatio: CGFloat
/// ๊ทธ๋ฐ๊ฑฐ ๋ชจ๋ฅด๊ฒ ๊ณ Scale๋ก ์ฌ์ด์ฆ ์กฐ์ ํ๊ณ ์ถ์ผ๋ฉด ์ฌ๊ธฐ
var scale: CGFloat
var onCircleTappedHandler: (CircleData) -> Void
var onCircleDismissedHandler: (CircleData) -> Void
public init(
onCircleTappedHandler: @escaping (CircleData) -> Void = { _ in },
onCircleDismissedHandler: @escaping (CircleData) -> Void = { _ in }
) {
activateCircleBlink = true
background = .white
outboundLength = 700
framePadding = 350
magnifiedCircleRatio = 0.9
scale = 1
self.onCircleTappedHandler = onCircleTappedHandler
self.onCircleDismissedHandler = onCircleDismissedHandler
}
public init(
activateCircleBlink: Bool,
background: Color,
outboundLength: CGFloat,
framePadding: CGFloat,
magnifiedCircleRatio: CGFloat,
scale: CGFloat,
onCircleTappedHandler: @escaping (CircleData) -> Void = { _ in },
onCircleDismissedHandler: @escaping (CircleData) -> Void = { _ in }
) {
self.activateCircleBlink = activateCircleBlink
self.background = background
self.outboundLength = outboundLength
self.framePadding = framePadding
self.magnifiedCircleRatio = magnifiedCircleRatio
self.scale = scale
self.onCircleTappedHandler = onCircleTappedHandler
self.onCircleDismissedHandler = onCircleDismissedHandler
}
}
public struct CirclePackView<DetailView: View>: View {
private let namespace: Namespace.ID
// ์ ๋๋ฉ์ด์
๊ด๋ จ
@State private var doneDragging = true
@State private var currentSheet: SheetPosition = .middle
@State private var currentSheetOffset: CGFloat = 0
@State private var maxSheetOffset: CGFloat = 200
@State private var idealSheetHeight: CGFloat = 400
@State private var animationEnded: Bool = true
@State private var showMorePersonalitySheet: Bool = false
@State private var focusedCircleData: CircleData?
@State private var isPersonalityCirclePressed = false
private var circleData: [CircleData]
private let option: CirclePackViewOption<DetailView>
private let detailViewBuilder: (CircleData) -> DetailView
private let morePersonalitystore = Store(initialState: MorePersonalityFeature.State()) {
MorePersonalityFeature()
}
public init(
namespace: Namespace.ID,
data: [CircleData],
rotationAngle: Angle = .degrees(45),
@ViewBuilder detailViewBuilder: @escaping (CircleData) -> DetailView
) {
print("init CirclePackView")
self.option = .init()
self.namespace = namespace
self.circleData = data.rotate(degree: rotationAngle)
self.morePersonalitystore.send(.loadPersonality) // ๋์ค์ ์์
self.detailViewBuilder = detailViewBuilder
}
public var body: some View {
ZStack {
// ๋ฐฑ๊ทธ๋ผ์ด๋์ ๊น๋ฆฌ๋ ๋ฐฐ๊ฒฝ์
option.background
.ignoresSafeArea()
.zIndex(0)
// Circle pack ๋ฉ์ธ ๋ทฐ
// ์ ์ฒด ์คํฌ๋กค, ์์๋ณต๊ตฌ๋๋ ์ค ๋ค์ด๊ฐ ์์
ScrollView([.horizontal, .vertical], showsIndicators: false) {
ZStack {
ZStack {
ForEach(circleData) { data in
if data == focusedCircleData {
Circle().fill(.clear)
} else {
SubCircleView(
namespace: namespace,
outboundLength: option.outboundLength,
circleData: data,
onTapGesture: {
guard animationEnded else { return }
option.onCircleTappedHandler(data)
focusedCircleData = data
})
}
}
}
.frame(width: option.outboundLength, height: option.outboundLength)
.padding(option.framePadding)
.scaleEffect(scale)
}
.pinchZooming()
}
.zIndex(1)
// ์๋์ ๊น๋ฆฐ ๋ทฐ ๋ธ๋ฌ์ํค๋ ํน์ ๋ทฐ
// `opacity`๋ฅผ ์ด์ฉํด์ visibility ์กฐ์ ํจ
BackgroundBlurringView(style: .dark)
.ignoresSafeArea()
.zIndex(1.5)
.opacity(focusedCircleData == nil ? 0 : 1)
.onTapGesture(perform: onDismiss)
// ๊ฐ๋ณ๋ณด๊ธฐ
VStack(alignment: .center, spacing: 0) {
if let focusedCircleData {
Group {
if isPersonalityCirclePressed {
ScoreAndPersonalityView(
title: "๋์ ์ ์",
score: focusedCircleData.metadata.myScore)
} else {
ScoreAndPersonalityView(
circleData: focusedCircleData)
}
}
.transition(.move(edge: .top).combined(with: .opacity))
FocusedCircleView(
namespace: namespace,
shrinkageDistance: currentSheetOffset,
maxShrinkageDistance: maxSheetOffset,
outboundLength: UIScreen.main.bounds.width * option.magnifiedCircleRatio,
blinkCircle: option.activateCircleBlink,
circleData: focusedCircleData)
.onDragChanged(self.onDragChanged)
.onDragEnded(self.onDragEnded)
.onLongPressStarted {
isPersonalityCirclePressed = true
}
.onLongPressEnded {
isPersonalityCirclePressed = false
}
.transition(.offset(x: 1, y: 1).combined(with: .opacity))
.padding(.vertical, 12)
VStack {
BottomSheetWrapperView {
detailViewBuilder(focusedCircleData)
}
.onDragChanged(self.onDragChanged)
.onDragEnded(self.onDragEnded)
}
.frame(
minWidth: UIScreen.main.bounds.width,
maxWidth: UIScreen.main.bounds.width,
idealHeight: idealSheetHeight
)
.cornerRadius(16, corners: [.topLeft, .topRight])
.transition(.move(edge: .bottom))
}
}
.frame(width: UIScreen.main.bounds.width)
.ignoresSafeArea(edges: [.bottom])
.zIndex(2)
// ์ฑ๊ฒฉ ๋๋ณด๊ธฐ
// morePersonalityButton
// .zIndex(2.5)
// .opacity(focusedCircleData == nil ? 1 : 0)
}
.animation(
Animation.customInteractiveSpring(),
value: isPersonalityCirclePressed)
.animation(
Animation.customInteractiveSpring(),
value: focusedCircleData)
.animation(
Animation.customInteractiveSpring(),
value: doneDragging)
.onChange(of: focusedCircleData) { _ in
animationEnded = false
// ์ ๋๋ฉ์ด์
๋๋๋ ํ์ด๋ฐ ์ก๊ธฐ ๊ท์ฐฎ์์ ๋์ถฉ ์๊ฐ ๊ณ์ฐํด์ ์ ์ด๋จ๋๋ฐ ๋์ค์ ๋ค์ด๋ด๊ฒ ์(๊ณผ์ฐ..?)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.55) {
self.animationEnded = true
}
}
.fullScreenCover(isPresented: $showMorePersonalitySheet) {
FocusedCircleOverlayView(
focusedCircle: CircleData.emptyCircle(radius: 0.9),
maxShrinkageDistance: maxSheetOffset,
detailViewBuilder: {
MorePersonalityView(store: morePersonalitystore)
})
.backgroundColor(DSKitAsset.Color.keymeBlack.swiftUIColor)
.showTopBar(true)
.onDismiss {
self.showMorePersonalitySheet = false
}
}
}
}
private extension CirclePackView {
/// ์ฑ๊ฒฉ ๋๋ณด๊ธฐ (์๋ ๋์ค์ ๋ฐ์ผ๋ก ๋ถ๋ฆฌ)
var morePersonalityButton: some View {
VStack(alignment: .center) {
Spacer()
HStack {
Spacer()
Button(action: {
showMorePersonalitySheet = true
print("Tapped")
}) {
VStack {
Text.keyme("๋ด ์ฑ๊ฒฉ ๋๋ณด๊ธฐ", font: .body3Semibold)
UpArrowButton()
.frame(width: 24, height: 24)
}
.frame(height: 52)
.foregroundColor(.white)
}
.frame(width: 135, height: 75)
.padding(.bottom, 18)
.contentShape(Rectangle())
Spacer()
}
}
.fullFrame()
// .background(
// // ์์์ ์ฝ 3/4 ์ง์ ์์ ์์ํ๋ ๊ทธ๋๋์ธํธ
// LinearGradient(
// colors: [.clear, .black],
// startPoint: .init(x: 0, y: 0.7),
// endPoint: .init(x: 0, y: 1))
// .allowsHitTesting(false))
}
}
private extension CirclePackView {
func onDragChanged(_ value: DragGesture.Value) {
doneDragging = false
currentSheetOffset =
currentSheet.position + value.translation.height.between(
min: -maxSheetOffset,
max: maxSheetOffset)
}
func onDragEnded(_ value: DragGesture.Value) {
let velocity = CGSize(
width: value.predictedEndLocation.x - value.location.x,
height: value.predictedEndLocation.y - value.location.y
).height
let velocityThreshold: CGFloat = 150
switch velocity {
case _ where velocity > velocityThreshold:
currentSheet = currentSheet.previous()
case _ where velocity < -velocityThreshold:
currentSheet = currentSheet.next()
default:
HapticManager.shared.unexpectedDelight()
}
currentSheetOffset = currentSheet.position
doneDragging = true
if case .dismiss = currentSheet { onDismiss() }
}
func onDismiss() {
guard animationEnded else { return }
if let focusedCircleData {
option.onCircleDismissedHandler(focusedCircleData)
}
focusedCircleData = nil
currentSheetOffset = 0
currentSheet = .middle
}
enum SheetPosition: CaseIterable {
case dismiss
case middle
case high
var position: CGFloat {
switch self {
case .high:
return -200
case .middle:
return 0
case .dismiss:
return 0
}
}
}
}
extension CirclePackView {
func activateCircleBlink(_ activated: Bool) -> CirclePackView {
self.option.activateCircleBlink = activated
return self
}
/// ๊ทธ๋ํ์ ๋ฐฑ๊ทธ๋ผ์ด๋ ์ปฌ๋ฌ๋ฅผ ์ค์ ํจ
func graphBackgroundColor(_ color: Color) -> CirclePackView {
self.option.background = color
return self
}
/// ํ๋ ์์ ํฌ๊ธฐ๋ฅผ ์ค์ ํฉ๋๋ค.
///
/// ๊ฐ๋ก-์ธ๋ก๊ฐ ๋์ผํ ์ ์ฌ๊ฐํ์ด๋ฏ๋ก ํ ๊ฐ์ ๊ธธ์ด๋ง ๋ฐ์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ 350์
๋๋ค.
func graphFrame(length: CGFloat) -> CirclePackView {
self.option.outboundLength = length
return self
}
/// ์ ์ฒด ํ๋ ์๊ณผ ๊ทธ๋ํ ์ฌ์ด ๊ฑฐ๋ฆฌ๋ฅผ ์ค์ ํฉ๋๋ค.
///
/// ๊ธฐ๋ณธ๊ฐ์ 350์
๋๋ค.
func graphFramePadding(_ factor: CGFloat) -> CirclePackView {
self.option.framePadding = factor
return self
}
/// ๋ฒ๋ธ์ฐจํธ์ ์ ํฌ๊ธฐ ์ค์ผ์ผ์ ์ก์ต๋๋ค.
///
/// ๊ธฐ๋ณธ๊ฐ์ ๋น์ฐํ 1์
๋๋ค.
func graphScale(_ factor: CGFloat) -> CirclePackView {
self.option.scale = factor
return self
}
func onCircleTapped(_ handler: @escaping (CircleData) -> Void) -> CirclePackView {
self.option.onCircleTappedHandler = handler
return self
}
func onCircleDismissed(_ handler: @escaping (CircleData) -> Void) -> CirclePackView {
self.option.onCircleDismissedHandler = handler
return self
}
}
private extension Array where Element == CircleData {
func rotate(degree: Angle) -> [CircleData] {
func formula(xPoint: CGFloat, yPoint: CGFloat) -> (x: CGFloat, y: CGFloat) {
let degree = CGFloat(degree.degrees)
let newXPoint = xPoint * cos(degree) - yPoint * sin(degree)
let newYPoint = yPoint * cos(degree) + xPoint * sin(degree)
return (newXPoint, newYPoint)
}
return self.map { data in
let newCoordinate = formula(xPoint: data.xPoint, yPoint: data.yPoint)
let newCircle = CircleData(
color: data.color,
xPoint: newCoordinate.x,
yPoint: newCoordinate.y,
radius: data.radius,
metadata: data.metadata)
return newCircle
}
}
}