-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFaceTimePiPView.swift
285 lines (251 loc) · 7.73 KB
/
FaceTimePiPView.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
//
// FaceTimePiPView.swift
// FluidInterfacesSwiftUI
//
// Created by Frad LEE on 6/3/21.
//
import SwiftUI
// MARK: - FaceTimePiPView
/// A re-creation of the picture-in-picture UI of the iOS FaceTime app.
///
/// # Key Features
///
/// 1. Light weight, airy interaction.
/// 2. Continuous animation that respects the gesture’s initial velocity.
///
/// # Design Theory
///
/// This drawer shows the concept of rewarding momentum. When the user swipes a view with
/// velocity, it’s much more satisfying to animate the view with bounciness. This makes the interface
/// feel alive and fun.
///
/// When the drawer is tapped, it animates without bounciness, which feels appropriate, since a tap
/// has no momentum in a particular direction.
///
/// When designing custom interactions, it’s important to remember that interfaces can have
/// different animations for different interactions.
///
/// # References
///
/// - [Building Fluid Interfaces. How to create natural gestures and…](https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5)
///
///
/// # To Do
///
/// - Use `PreferenceKey` rewrite this view, [read more](https://swiftwithmajid.com/2020/01/15/the-magic-of-view-preferences-in-swiftui/ ).
struct FaceTimePiPView: View {
// MARK: Internal
var body: some View {
ZStack {
GeometryReader { geometry in
let geometryWidth = geometry.size.width
let geometryHeight = geometry.size.height
background
filledRoundedRectangle
.offset(x: currentPosition.width, y: currentPosition.height)
.gesture(
DragGesture()
.onChanged { value in
self.currentPosition = CGSize(
width: value.translation.width + self.newPosition.width,
height: value.translation.height + self.newPosition.height
)
}
.onEnded { value in
self.predictedEndLocation = value.predictedEndLocation
// -TODO: Better velocity calculation to make it range [0, 1].
let currentVelocityX =
(value.predictedEndLocation.x - value.location.x)
/ value.predictedEndLocation.x
let currentVelocityY =
(value.predictedEndLocation.y - value.location.y)
/ value.predictedEndLocation.y
self.currentVelocity =
sqrt(pow(currentVelocityX, 2) + pow(currentVelocityY, 2))
self.roundedRectanglePosition =
findRoundedRectanglePosition(geometry, predictedEndLocation)
?? .topLeading
switch roundedRectanglePosition {
case .topLeading:
newPosition = CGSize(width: 0, height: 0)
case .topTrailing:
newPosition = CGSize(width: geometryWidth - 120, height: 0)
case .bottomLeading:
newPosition = CGSize(width: 0, height: geometryHeight - 180)
case .bottomTrailing:
newPosition = CGSize(
width: geometryWidth - 120,
height: geometryHeight - 180
)
}
withAnimation(.interpolatingSpring(
mass: 1.0,
stiffness: 134.0,
damping: 16.0,
initialVelocity: Double(currentVelocity)
)) {
self.currentPosition = self.newPosition
}
}
)
}
debugView
}
.padding()
}
// MARK: Private
private enum RoundedRectanglePosition {
/// `-----`
/// `|x| |`
/// `-----`
/// `| | |`
/// `-----`
case topLeading
/// `-----`
/// `| |x|`
/// `-----`
/// `| | |`
/// `-----`
case topTrailing
/// `-----`
/// `| | |`
/// `-----`
/// `|x| |`
/// `-----`
case bottomLeading
/// `-----`
/// `| | |`
/// `-----`
/// `| |x|`
/// `-----`
case bottomTrailing
}
@Environment(\.colorScheme) private var colorScheme
@State private var roundedRectanglePosition: RoundedRectanglePosition =
.topLeading
@State private var predictedEndLocation: CGPoint = .zero
@State private var currentVelocity: CGFloat = .zero
@State private var currentPosition: CGSize = .zero
@State private var newPosition: CGSize = .zero
/// Find the right corner which rounded rectangle should be pinned.
/// - Parameters:
/// - geometry: Size of the container view.
/// - currentPosition: Current poistion of rounded rectangle.
/// - Returns: The corner which rounded rectangle should be pinned.
private func findRoundedRectanglePosition(
_ geometry: GeometryProxy,
_ predictedEndLocation: CGPoint
) -> RoundedRectanglePosition? {
/// `GeometryProxy` X / width
let geometryX = geometry.size.width
/// `GeometryProxy` Y / height
let geometryY = geometry.size.height
/// Predicted End Location X
let predictedX = predictedEndLocation.x
/// Predicted End Location Y
let predictedY = predictedEndLocation.y
// - TODO: Fix the CHAOS logic.
if predictedX < geometryX / 2,
predictedY < geometryY / 2 {
return .topLeading
} else if predictedX > geometryX / 2,
predictedY < geometryY / 2 {
return .topTrailing
} else if predictedX < geometryX / 2,
predictedY > geometryY / 2 {
return .bottomLeading
} else if predictedX > geometryX / 2,
predictedY > geometryY / 2 {
return .bottomTrailing
} else {
// -TODO: Catch the error
return nil
}
}
}
extension FaceTimePiPView {
var roundedRectangle: some View {
RoundedRectangleView(isFiiled: false)
}
var filledRoundedRectangle: some View {
RoundedRectangleView(isFiiled: true)
}
var background: some View {
HStack {
VStack {
roundedRectangle
Spacer()
roundedRectangle
}
Spacer()
VStack {
roundedRectangle
Spacer()
roundedRectangle
}
}
}
var debugView: some View {
VStack(alignment: .center, spacing: 8) {
Text("Current Velocity")
.textCase(.uppercase)
FormatedNumView(num: $currentVelocity)
Text("Current Position")
.textCase(.uppercase)
HStack(spacing: 0) {
Text("(")
FormatedNumView(num: $currentPosition.height)
Text(", ")
FormatedNumView(num: $currentPosition.width)
Text(")")
}
.animation(nil)
}
.foregroundColor(.white)
.padding()
.background(RoundedRectangle(cornerRadius: 8)
.fill(
colorScheme == .dark ?
Color.white.opacity(0.3) : Color.black.opacity(0.3)
))
}
}
// MARK: - RoundedRectangleView
struct RoundedRectangleView: View {
@State var isFiiled: Bool
var body: some View {
let linearGradient = LinearGradient(
gradient: Gradient(colors: [.topColor, .bottomColor]),
startPoint: .top,
endPoint: .bottom
)
Group {
if isFiiled {
RoundedRectangle(cornerRadius: 8)
.fill(linearGradient)
} else {
RoundedRectangle(cornerRadius: 8)
.fill(Color.clear)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color.gray, lineWidth: 2)
)
}
}
.frame(
width: 120,
height: 180,
alignment: .center
)
}
}
private extension Color {
static let topColor = Color(red: 0.95, green: 0.95, blue: 0.23)
static let bottomColor = Color(red: 0.97, green: 0.65, blue: 0.11)
}
// MARK: - FaceTimePiPView_Previews
struct FaceTimePiPView_Previews: PreviewProvider {
static var previews: some View {
FaceTimePiPView()
}
}