-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeCard.swift
116 lines (103 loc) · 4.39 KB
/
HomeCard.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
//
// HomeCard.swift
// Eule
//
// Created by Hariom Palkar on 16/11/20.
// Copyright © 2020 Hariom Palkar. All rights reserved.
//
import SwiftUI
struct HomeCardView: View {
@State private var translation: CGSize = .zero
@State private var swipeStatus: LikeDislike = .none
private var card: HomeCard
private var onRemove: (_ card: HomeCard) -> Void
private var thresholdPercentage: CGFloat = 0.5
// when the user has draged 50% the width of the screen in either direction
private enum LikeDislike: Int {
case like, dislike, none
}
init(card: HomeCard, onRemove: @escaping (_ card: HomeCard) -> Void) {
self.card = card
self.onRemove = onRemove
}
/// What percentage of our own width have we swipped
/// - Parameters:
/// - geometry: The geometry
/// - gesture: The current gesture translation value
private func getGesturePercentage(_ geometry: GeometryProxy, from gesture: DragGesture.Value) -> CGFloat {
gesture.translation.width / geometry.size.width
}
var body: some View {
GeometryReader { geometry in
VStack(alignment: .leading) {
ZStack(alignment: self.swipeStatus == .like ? .topLeading : .topTrailing) {
Image(self.card.image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: geometry.size.width, height: geometry.size.height * 0.75)
.clipped()
if self.swipeStatus == .like {
Text("LIKE")
.font(.headline)
.padding()
.cornerRadius(10)
.foregroundColor(Color.green)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.green, lineWidth: 3.0)
).padding(24)
.rotationEffect(Angle.degrees(-45))
} else if self.swipeStatus == .dislike {
Text("DISLIKE")
.font(.headline)
.padding()
.cornerRadius(10)
.foregroundColor(Color.red)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.red, lineWidth: 3.0)
).padding(.top, 45)
.rotationEffect(Angle.degrees(45))
}
}
}
.background(Color.white)
.cornerRadius(10)
.shadow(radius: 5)
.animation(.interactiveSpring())
.offset(x: self.translation.width, y: 0)
.rotationEffect(.degrees(Double(self.translation.width / geometry.size.width) * 25), anchor: .top)
.gesture(
DragGesture()
.onChanged { value in
self.translation = value.translation
if (self.getGesturePercentage(geometry, from: value)) >= self.thresholdPercentage {
self.swipeStatus = .like
} else if self.getGesturePercentage(geometry, from: value) <= -self.thresholdPercentage {
self.swipeStatus = .dislike
} else {
self.swipeStatus = .none
}
}.onEnded { value in
// determine snap distance > 0.5 aka half the width of the screen
if abs(self.getGesturePercentage(geometry, from: value)) > self.thresholdPercentage {
self.onRemove(self.card)
} else {
self.translation = .zero
}
}
)
}
}
}
// 7
struct HomeCardView_Previews: PreviewProvider {
static var previews: some View {
HomeCardView(card: HomeCard(id: 0, image: "", title: "", link: "", desc: ""),
onRemove: { _ in
// do nothing
})
.frame(height: 250)
.padding()
}
}