Created
December 5, 2019 10:31
-
-
Save anupamchugh/c512d8c9da7db62c1d54655f4f31a3e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView : View { | |
@EnvironmentObject var observer : SwipeObserver | |
var body : some View{ | |
GeometryReader{geo in | |
ZStack{ | |
ForEach(self.observer.cards){card in | |
Rectangle() | |
.foregroundColor(card.color) | |
.cornerRadius(20) | |
.frame(width: geo.size.width-40, height: geo.size.height - 80, alignment: .center) | |
.gesture(DragGesture() | |
.onChanged({ (value) in | |
if value.translation.width > 0{ | |
if value.translation.width > 30{ | |
self.observer.update(id: card, value: value.translation.width, degree: 12) | |
} | |
else{ | |
self.observer.update(id: card, value: value.translation.width, degree: 0) | |
} | |
} | |
else{ | |
if value.translation.width < -30{ | |
self.observer.update(id: card, value: value.translation.width, degree: -12) | |
} | |
else{ | |
self.observer.update(id: card, value: value.translation.width, degree: 0) | |
} | |
} | |
}).onEnded({ (value) in | |
if card.drag > 0{ | |
if card.drag > geo.size.width / 2 - 40{ | |
self.observer.update(id: card, value: 500, degree: 0) | |
} | |
else{ | |
self.observer.update(id: card, value: 0, degree: 0) | |
} | |
} | |
else{ | |
if -card.drag > geo.size.width / 2 - 40{ | |
self.observer.update(id: card, value: -500, degree: 0) | |
} | |
else{ | |
self.observer.update(id: card, value: 0, degree: 0) | |
} | |
} | |
}) | |
).offset(x: card.drag) | |
.scaleEffect(abs(card.drag) > 100 ? 0.8 : 1) | |
.rotationEffect(.init(degrees:card.degree)) | |
.animation(.spring()) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@anupamchugh could you attach the code for
SwipeObserver
as well? would be interested in handling changing the data based on swipes too if you ever implemented that. :)