-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnboardingView.swift
More file actions
256 lines (221 loc) · 8.45 KB
/
OnboardingView.swift
File metadata and controls
256 lines (221 loc) · 8.45 KB
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
// Copyright © 2025 Booket. All rights reserved
import BKDesign
import Combine
import SnapKit
import UIKit
struct OnboardingPage {
let image: UIImage
let title: String
let description: String
let titleHighlightWord: String
}
final class OnboardingView: BaseView {
let eventPublisher = PassthroughSubject<OnboardingViewEvent, Never>()
private let containerView = UIView()
private let scrollView = UIScrollView()
private let innerContentView = UIView()
private let innerStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.spacing = .zero
stackView.distribution = .fillEqually
return stackView
}()
private let pageControl: UIPageControl = {
let pageControl = UIPageControl()
pageControl.currentPage = .zero
pageControl.pageIndicatorTintColor = .bkBackgroundColor(.secondaryPressed)
pageControl.currentPageIndicatorTintColor = .bkBackgroundColor(.primary)
pageControl.numberOfPages = Constants.pages.count
return pageControl
}()
private let nextButton = BKButtonGroup.singleFullButton(title: "다음")
override func setupView() {
addSubviews(containerView)
containerView.addSubviews(innerContentView, pageControl, nextButton)
innerContentView.addSubview(scrollView)
scrollView.addSubview(innerStackView)
Constants.pages.map(makeInnerView).forEach { pageView in
innerStackView.addArrangedSubview(pageView)
pageView.snp.makeConstraints {
$0.width.equalTo(scrollView.frameLayoutGuide)
$0.height.equalTo(scrollView.frameLayoutGuide)
}
}
}
override func configure() {
scrollView.isPagingEnabled = true
scrollView.alwaysBounceVertical = false
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
pageControl.addTarget(self, action: #selector(pageControlChanged), for: .valueChanged)
nextButton.primaryButton?.addTarget(self, action: #selector(nextButtonTapped), for: .touchUpInside)
}
override func setupLayout() {
containerView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
nextButton.snp.makeConstraints {
$0.leading.trailing.equalToSuperview()
$0.bottom.equalTo(safeAreaLayoutGuide.snp.bottom)
}
pageControl.snp.makeConstraints {
$0.centerX.equalToSuperview()
$0.top.equalTo(nextButton.snp.top)
.offset(-36)
}
innerContentView.snp.makeConstraints {
$0.top.equalTo(safeAreaLayoutGuide.snp.top)
$0.leading.trailing.equalToSuperview()
$0.bottom.equalTo(pageControl.snp.top)
}
scrollView.snp.makeConstraints {
$0.centerY.equalToSuperview()
$0.leading.trailing.equalToSuperview()
$0.height.lessThanOrEqualTo(innerContentView.snp.height)
}
innerStackView.snp.makeConstraints {
$0.edges.equalTo(scrollView.contentLayoutGuide)
$0.width.equalTo(scrollView.frameLayoutGuide)
.multipliedBy(pageControl.numberOfPages)
$0.height.equalTo(scrollView.frameLayoutGuide)
}
}
}
private extension OnboardingView {
func makeInnerView(
page: OnboardingPage
) -> UIView {
let contentView = UIView()
let imageView = UIImageView(image: page.image)
let labelStack = makeInnerLabelStack(page)
contentView.addSubviews(imageView, labelStack)
imageView.contentMode = .scaleAspectFit
imageView.snp.makeConstraints {
$0.top.equalToSuperview()
$0.horizontalEdges.equalToSuperview()
$0.height.equalTo(imageView.snp.width).multipliedBy(274.0 / 375.0)
}
labelStack.snp.makeConstraints {
$0.top.equalTo(imageView.snp.bottom)
.offset(LayoutConstants.labelStackTopOffset)
$0.leading.trailing.equalToSuperview()
.inset(LayoutConstants.labelStackHorizontalInset)
$0.bottom.equalToSuperview()
}
return contentView
}
func makeInnerLabelStack(
_ page: OnboardingPage
) -> UIStackView {
let titleLabel = BKLabel(
text: page.title,
fontStyle: .heading1(weight: .bold),
alignment: .center,
highlightedWord: page.titleHighlightWord
)
let descriptionLabel = BKLabel(
text: page.description,
fontStyle: .body2(weight: .medium),
color: .bkContentColor(.tertiary),
alignment: .center
)
titleLabel.numberOfLines = .zero
descriptionLabel.numberOfLines = .zero
let labelStack = UIStackView()
labelStack.axis = .vertical
labelStack.spacing = LayoutConstants.labelStackSpacing
labelStack.alignment = .center
[titleLabel, descriptionLabel].forEach(labelStack.addArrangedSubview(_:))
return labelStack
}
@objc func pageControlChanged(_ sender: UIPageControl) {
let x = CGFloat(sender.currentPage) * scrollView.bounds.width
scrollView.setContentOffset(.init(x: x, y: 0), animated: true)
}
@objc func nextButtonTapped() {
let next = pageControl.currentPage + 1
guard next < pageControl.numberOfPages else {
eventPublisher.send(.onboardingDidFinish)
return
}
updateButtonTitle(for: next)
updatePage(to: next)
}
func updateButtonTitle(for page: Int) {
let isLast = (page == pageControl.numberOfPages - 1)
let title = isLast ? "시작하기" : "다음"
nextButton.primaryButton?.title = title
}
func updatePage(to index: Int) {
pageControl.currentPage = index
pageControlChanged(pageControl)
}
func syncPageWithScroll() {
guard scrollView.bounds.width > 0 else { return }
let page = Int(round(scrollView.contentOffset.x / scrollView.bounds.width))
guard page != pageControl.currentPage,
(0..<pageControl.numberOfPages).contains(page) else { return }
pageControl.currentPage = page
updateButtonTitle(for: page)
}
}
extension OnboardingView: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
syncPageWithScroll()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
syncPageWithScroll()
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
syncPageWithScroll()
}
}
private extension OnboardingView {
enum LayoutConstants {
static let labelStackSpacing = BKSpacing.spacing3
static let labelStackTopOffset = BKSpacing.spacing8
static let labelStackHorizontalInset = BKInset.inset5
}
enum Constants {
static let pages: [OnboardingPage] = [
OnboardingPage(
image: BKImage.Graphics.onboarding1,
title: """
읽고 있는 책을 등록하고
바로 기록해보세요
""",
description: """
책을 덮기 전, 마음에 남은 문장과
감정을 간편하게 남길 수 있어요
""",
titleHighlightWord: "기록"
),
OnboardingPage(
image: BKImage.Graphics.onboarding2,
title: """
독서 중 느낀 감정을
자세히 남겨 보세요
""",
description: """
책마다 쌓인 감정들은
나만의 독서 흔적이 됩니다
""",
titleHighlightWord: "감정"
),
OnboardingPage(
image: BKImage.Graphics.onboarding3,
title: """
기록한 문장을
카드로 공유해 보세요
""",
description: """
감정 캐릭터와 함께
이미지로 저장하고 공유할 수 있어요
""",
titleHighlightWord: "공유"
)
]
}
}