-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTooltipView.swift
More file actions
134 lines (114 loc) · 3.7 KB
/
Copy pathTooltipView.swift
File metadata and controls
134 lines (114 loc) · 3.7 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
//
// ToolTipView.swift
// Presentation
//
// Created by 이동현 on 7/20/25.
//
import UIKit
import SnapKit
final class TooltipView: UIView {
enum TooltipTailPosition {
case center
case offsetFromLeading(CGFloat)
}
private enum Layout {
static let backgroundViewBottomInset: CGFloat = 8
static let messageLabelInset: CGFloat = 10
static let tailWidth: CGFloat = 14
static let tailHeight: CGFloat = 8
static let cornerRadius: CGFloat = 8
}
private let backgroundView = UIView()
private let messageLabel = UILabel()
private let tailLayer = CAShapeLayer()
private let tailPosition: TooltipTailPosition
init(tailPosition: TooltipTailPosition) {
self.tailPosition = tailPosition
super.init(frame: .zero)
configureAttribute()
configureLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
generateTail()
}
private func configureAttribute() {
backgroundView.backgroundColor = BitnagilColor.navy400
backgroundView.layer.cornerRadius = Layout.cornerRadius
messageLabel.textColor = .white
messageLabel.font = BitnagilFont
.init(style: .caption1, weight: .medium)
.font
.withSize(12)
}
private func configureLayout() {
addSubview(backgroundView)
backgroundView.addSubview(messageLabel)
backgroundView.snp.makeConstraints { make in
make.top.horizontalEdges.equalToSuperview()
make.bottom.equalToSuperview().inset(Layout.backgroundViewBottomInset)
}
messageLabel.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(Layout.messageLabelInset)
}
}
func configure(message: String) {
messageLabel.text = message
}
private func generateTail() {
let startX: CGFloat
let width = Layout.tailWidth
let height = Layout.tailHeight
let y = bounds.height - height
switch tailPosition {
case .center:
startX = (backgroundView.bounds.width - width) / 2
case .offsetFromLeading(let offset):
startX = offset
}
let path = UIBezierPath()
path.move(to: CGPoint(x: startX, y: y))
path.addQuadCurve(
to: CGPoint(x: startX + width, y: y),
controlPoint: CGPoint(x: startX + width / 2, y: y + height * 1.5))
path.addLine(to: CGPoint(x: startX + width, y: y))
path.close()
tailLayer.path = path.cgPath
tailLayer.fillColor = backgroundView.backgroundColor?.cgColor
if tailLayer.superlayer == nil {
layer.addSublayer(tailLayer)
}
}
func showTooltip() {
self.alpha = 0
self.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
self.isHidden = false
UIView.animate(
withDuration: 0.2,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: [.curveEaseOut],
animations: {
self.alpha = 1
self.transform = .identity
},
completion: nil)
}
func hideTooltip(completion: (() -> Void)? = nil) {
UIView.animate(
withDuration: 0.15,
delay: 0,
options: [.curveEaseIn],
animations: {
self.alpha = 0
self.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
}) { _ in
self.isHidden = true
self.transform = .identity
}
}
}