Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created December 5, 2019 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/c512d8c9da7db62c1d54655f4f31a3e5 to your computer and use it in GitHub Desktop.
Save anupamchugh/c512d8c9da7db62c1d54655f4f31a3e5 to your computer and use it in GitHub Desktop.
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())
}
}
}
}
}
@malonehedges
Copy link

@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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment