/
TinderAnimation.swift
360 lines (325 loc) · 13.9 KB
/
TinderAnimation.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
//
// TinderAnimation.swift
// AnnimationWithSwiftUI
//
// Created by mac-00017 on 09/05/22.
//
import SwiftUI
struct TinderAnimation: View {
//MARK: - properties -
let userProfiles: UserProfiles
@State var cardViews = [CardView]()
@GestureState var dragState = DragState.inactive
var dragAreaThreshold: CGFloat = 65.0
@State var lastCardIndex = 1
@State var cardRemovaltransition = AnyTransition.trailingBottom
@State var isLiked: Bool = false
@State var isDisliked: Bool = false
//MARK: - Drag States
enum DragState {
case inactive
case pressing
case dragging(translation: CGSize)
var translation: CGSize {
switch self {
case .inactive, .pressing:
return .zero
case .dragging(let translation):
return translation
}
}
var isDragging:Bool {
switch self {
case .dragging:
return true
case .inactive, .pressing:
return false
}
}
var isPressing: Bool {
switch self {
case .pressing, .dragging :
return true
case .inactive:
return false
}
}
}
//MARK: - Body -
var body: some View {
return VStack{
//MARK: Header
HeaderView()
.animation(.default, value: dragState.isDragging)
//MARK: CardView
ZStack{
if let cardViews = cardViews {
ForEach(cardViews) { card in
card
.zIndex(self.isTopCard(cardView: card) ? 1 : 0)
.overlay(content: {
ZStack{
// X-MARK SYMBOL
Image(systemName: "x.circle")
.modifier(SymbolModifier())
.opacity(self.dragState.translation.width < -self.dragAreaThreshold && self.isTopCard(cardView: card) ? 1.0 : (isDisliked && self.isTopCard(cardView: card)) ? 1.0 : 0.0)
// HEART SYMBOL
Image(systemName: "heart.circle")
.modifier(SymbolModifier())
.opacity(self.dragState.translation.width > self.dragAreaThreshold && self.isTopCard(cardView: card) ? 1.0 : (isLiked && self.isTopCard(cardView: card)) ? 1.0 : 0.0)
}
})
// Gesture Animation
.offset(x: self.isTopCard(cardView: card) ? self.dragState.translation.width : 0, y: self.isTopCard(cardView: card) ? self.dragState.translation.height : 0)
.scaleEffect(self.dragState.isDragging && self.isTopCard(cardView: card) ? 0.85 : 1)
.rotationEffect(Angle(degrees:self.isTopCard(cardView: card) ? Double(self.dragState.translation.width / 12) : 0))
.animation(.interpolatingSpring(stiffness: 120, damping: 120), value: self.dragState.translation)
.gesture(LongPressGesture(minimumDuration: 0.01)
.sequenced(before: DragGesture())
.updating(self.$dragState, body: { value, state, transaction in
switch value {
case .first(true):
state = .pressing
case .second(true, let drag):
state = .dragging(translation: drag?.translation ?? .zero)
default:
break
}
})
// .onChanged({ value in
// guard case .second(true, let drag?) = value else {return}
// if drag.translation.width < -self.dragAreaThreshold {
// self.cardRemovaltransition = .leadingBottom
// }
//
// if drag.translation.width > self.dragAreaThreshold {
// self.cardRemovaltransition = .trailingBottom
// }
//
// })
.onEnded({ value in
guard case .second(true, let drag?) = value else {
return
}
if drag.translation.width < -self.dragAreaThreshold || drag.translation.width > self.dragAreaThreshold {
withAnimation(.easeOut) {
self.moveCards()
}
}
})
)
.scaleEffect((self.isTopCard(cardView: card) && (isLiked || isDisliked)) ? 0.9 : 1)
.transition(.offset(x: (self.isTopCard(cardView: card) && isLiked) ? 400 : (self.isTopCard(cardView: card) && isDisliked) ? -400 : 0, y: (self.isTopCard(cardView: card) && (isLiked || isDisliked)) ? 400 : 0))
.onChange(of: isLiked) { liked in
cardRemovaltransition = .trailingBottom
if self.isTopCard(cardView: card) {
if liked {
isLiked.toggle()
print(cardRemovaltransition)
self.moveCards()
}
}
}
.onChange(of: isDisliked) { disliked in
cardRemovaltransition = .leadingBottom
if self.isTopCard(cardView: card) {
if disliked {
isDisliked.toggle()
print(cardRemovaltransition)
self.moveCards()
}
}
}
.animation(.easeInOut(duration: 0.5),value: (isLiked || isDisliked))
.scaleEffect((self.isTopCard(cardView: card) && (isLiked || isDisliked)) ? 0.6 : 1)
.rotationEffect(Angle(degrees:(self.isTopCard(cardView: card) && isLiked) ? 65 : (self.isTopCard(cardView: card) && isDisliked) ? -65 : 0))
.animation(.easeOut(duration: 0.8)
.delay(0.5),
value: (isLiked || isDisliked))
}
}
}
Spacer()
//MARK: Footer
FooterView(isLiked: $isLiked, isDisLiked: $isDisliked)
}
.padding(.bottom, 20)
.edgesIgnoringSafeArea(.bottom)
.onAppear {
self.cardViews = loadCardViewData()
}
}
private func moveCards() {
cardViews.removeFirst()
self.lastCardIndex += 1
if let profileCard = userProfiles.results?[lastCardIndex % (userProfiles.results?.count ?? 0)] {
let newCarView = CardView(user: profileCard,isLiked: Binding.constant(false))
cardViews.append(newCarView)
}
}
private func loadCardViewData() -> [CardView] {
var views = [CardView]()
if let userprofiles = userProfiles.results {
for index in 0..<2 {
views.append(CardView(user: userprofiles[index],isLiked: Binding.constant(false)))
}
}
return views
}
private func isTopCard(cardView: CardView) -> Bool {
guard let index = cardViews.firstIndex(where: {$0.id == cardView.id}) else {return false}
return index == 0
}
}
struct CardView: View, Identifiable {
var id = UUID()
var user: Results
var style: UIBlurEffect.Style = .systemUltraThinMaterial
@Binding var isLiked: Bool
var body: some View {
ZStack{
ForEach(user.photos ?? []) { userProfile in
if userProfile.main == true {
Image(userProfile.url ?? "")
.resizable()
.aspectRatio(contentMode: .fit)
.scaledToFill()
.frame(minWidth: 0, maxWidth: .infinity)
.overlay(alignment: .bottom, content: {
ZStack{
VisualEffectView(style: style)
.cornerRadius(25)
.opacity(50)
VStack(alignment: .center){
HStack {
VStack(alignment: .leading){
Text(user.name ?? "Agne")
.font(.title)
.fontWeight(.semibold)
Text(user.gender ?? "female")
}
Spacer()
}
.padding(.leading, 20)
}
}
.frame(minHeight: 0 ,maxHeight: 100)
})
}
}
}
.cornerRadius(20)
.padding()
}
}
struct HeaderView: View {
var body: some View {
HStack {
Button {
print("Information")
} label: {
Image(systemName: "person.circle.fill")
.resizable()
.renderingMode(.original)
.scaledToFit()
.font(.system(size: 24, weight: .regular))
.frame(width: 40, height: 40, alignment: .center)
.padding()
}
.buttonStyle(StaticButtonStyle())
.tint(.primary)
Spacer()
}
.padding(.trailing, 110)
}
}
struct FooterView: View {
@Binding var isLiked: Bool
@Binding var isDisLiked: Bool
//MARK: - Body
var body: some View {
HStack{
Button {
isDisLiked.toggle()
} label: {
Image(systemName: "xmark.circle")
.renderingMode(.template)
.font(.system(size: 42, weight: .light))
.padding()
} //: Button
.buttonStyle(StaticButtonStyle())
.foregroundColor(.black)
Spacer()
Text("Swipe") //: Text
.foregroundColor(.pink)
.font(.system(size: 25, weight: .bold, design: .serif))
.padding(.horizontal, 20)
.padding(.vertical, 12)
.background(
Capsule().stroke(.pink, lineWidth: 2)
)
Spacer()
Button {
isLiked.toggle()
} label: {
Image(systemName: "heart.circle")
.renderingMode(.template)
.foregroundColor(.black)
.font(.system(size: 42, weight: .light))
.padding()
} //: Button
.buttonStyle(StaticButtonStyle())
} //: HStack
}
}
struct VisualEffectView: UIViewRepresentable {
typealias UIViewType = UIVisualEffectView
var style: UIBlurEffect.Style = .systemUltraThinMaterial
func makeUIView(context: Context) -> UIVisualEffectView {
let view = UIVisualEffectView(effect: UIBlurEffect(style: style))
return view
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
}
}
struct SymbolModifier: ViewModifier {
func body(content: Content) -> some View {
content
.foregroundColor(Color.white)
.font(.system(size: 128))
.shadow(color: Color(UIColor(red: 0, green: 0, blue: 0, alpha: 0.2)), radius: 12, x: 0, y: 0)
}
}
extension AnyTransition {
static var trailingBottom: AnyTransition {
AnyTransition.asymmetric(insertion: .identity, removal: AnyTransition.move(edge: .trailing).combined(with: .move(edge: .bottom)))
}
static var leadingBottom: AnyTransition {
AnyTransition.asymmetric(insertion: .identity, removal: AnyTransition.move(edge: .leading).combined(with: .move(edge: .bottom)))
}
}
extension Bundle {
func decode<T: Codable>(_ file: String) -> T {
// 1. Locate the json file
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
// 2. Create a property for the data
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}
// 3. Create a decoder
let decoder = JSONDecoder()
// 4. Create a property for the decoded data
guard let loaded = try? decoder.decode(T.self, from: data) else {
fatalError("Failed to decode \(file) from bundle.")
}
// 5. Return the ready-to-use data
return loaded
}
}
struct StaticButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
}
}