Skip to content

Commit

Permalink
add top and bottom swiping
Browse files Browse the repository at this point in the history
  • Loading branch information
omarthamri committed Nov 30, 2023
1 parent cec4949 commit 76e79aa
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/Tests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.6</string>
<string>1.7</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion MyTinderSwipingAnimation.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MyTinderSwipingAnimation'
s.version = '1.6'
s.version = '1.7'
s.summary = 'TinderSwipingAnimation is a library written using SwiftUI to simplify the implementation of the tinder swipe animation.'

# This description is used to generate tags and improve search results.
Expand Down
4 changes: 2 additions & 2 deletions Sources/TinderSwipingAnimation/Components/CustomButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ struct CustomButton: View {
case .goLeft:
viewModel.goLeft = true
case .goTop:
break
viewModel.goTop = true
case .goBottom:
break
viewModel.goBottom = true
}
}) {
button.image
Expand Down
4 changes: 4 additions & 0 deletions Sources/TinderSwipingAnimation/Models/Direction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ public enum Direction {

case right
case left
case top
case bottom
public var description: String {
switch self {
case .right: return "right"
case .left: return "left"
case .top: return "top"
case .bottom: return "bottom"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class TinderViewModel: ObservableObject {
@Published var cards: [CardModel] = []
@Published var goRight: Bool = false
@Published var goLeft: Bool = false
@Published var goTop: Bool = false
@Published var goBottom: Bool = false
}
41 changes: 34 additions & 7 deletions Sources/TinderSwipingAnimation/Views/CardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import SwiftUI
import Combine

struct CardView: View {
@State var x: [CGFloat] = [0,0,0,0,0,0,0]
@State var degree: [Double] = [0,0,0,0,0,0,0]
@State var x: [CGFloat] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
@State var y: [CGFloat] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
@State var degree: [Double] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
@State var offset: CGFloat = 0.0
var cards: [CardModel]
var buttons: [ButtonModel]
Expand All @@ -26,18 +27,24 @@ struct CardView: View {
viewModel.$goRight.sink { _ in
}
.store(in: &subscriptions)
viewModel.$goLeft.sink { _ in
viewModel.$goLeft.sink { _ in
}
.store(in: &subscriptions)
viewModel.$goTop.sink { _ in
}
.store(in: &subscriptions)
viewModel.$goBottom.sink { _ in
}
.store(in: &subscriptions)
}
var body: some View {
ZStack {
ForEach(0..<cards.count,id: \.self) { i in
Card(card: cards[i], buttons: buttons, viewModel: viewModel, orientation: orientation)
.offset(x: self.x[i])
.offset(x: self.x[i],y: y[i])
.rotationEffect(.init(degrees: degree[i]))
.onChange(of: viewModel.goRight || viewModel.goLeft) {
if viewModel.goRight && abs(degree[i]) != 12 {
.onChange(of: viewModel.goRight || viewModel.goLeft || viewModel.goBottom || viewModel.goTop) {
if viewModel.goRight && abs(degree[i]) != 12 && abs(degree[i]) != 1 {
withAnimation(.default) {
viewModel.goRight = false
self.x[i] = 500
Expand All @@ -47,7 +54,7 @@ struct CardView: View {
viewModel.cards[i].thumbsUpOpacity = 0
viewModel.cards[i].thumbsDownOpacity = 0
}
} else if viewModel.goLeft && abs(degree[i]) != 12 {
} else if viewModel.goLeft && abs(degree[i]) != 12 && abs(degree[i]) != 1 {
withAnimation(.default) {
viewModel.goLeft = false
self.x[i] = -500
Expand All @@ -57,6 +64,26 @@ struct CardView: View {
viewModel.cards[i].thumbsUpOpacity = 0
viewModel.cards[i].thumbsDownOpacity = 0
}
} else if viewModel.goTop && abs(degree[i]) != 1 && abs(degree[i]) != 12{
withAnimation(.default) {
viewModel.goTop = false
self.y[i] = 750
self.offset = 750
self.degree[i] = 1
viewModel.cardSwiped = (cards[i],Direction.top)
viewModel.cards[i].thumbsUpOpacity = 0
viewModel.cards[i].thumbsDownOpacity = 0
}
} else if viewModel.goBottom && abs(degree[i]) != 1 && abs(degree[i]) != 12 {
withAnimation(.default) {
viewModel.goBottom = false
self.y[i] = -750
self.offset = -750
self.degree[i] = -1
viewModel.cardSwiped = (cards[i],Direction.bottom)
viewModel.cards[i].thumbsUpOpacity = 0
viewModel.cards[i].thumbsDownOpacity = 0
}
}
}
.gesture(DragGesture()
Expand Down

0 comments on commit 76e79aa

Please sign in to comment.