Skip to content

Instantly share code, notes, and snippets.

@Viranchee
Created April 23, 2020 19:35
Show Gist options
  • Save Viranchee/63ae364ade6d161ca7f7e308b4a14646 to your computer and use it in GitHub Desktop.
Save Viranchee/63ae364ade6d161ca7f7e308b4a14646 to your computer and use it in GitHub Desktop.
SwiftBounty
import SwiftUI
import PlaygroundSupport
extension View {
func debug(_ value: Any) -> Self {
dump(value)
return self
}
}
struct CardSwiper: View {
@State var dragMovement: CGSize = .zero
@State var combined: CGSize = .zero
@State var angleFloat: CGFloat = .zero
@State var gestureActive: Bool = false
var scale: CGFloat {
return gestureActive ? 1.05 : 1
}
var angle: Angle { Angle.init(degrees: Double(
sqrt(pow(dragMovement.height, 2) + pow(dragMovement.width, 2)) * 0.1 * dragMovement.width/(abs(dragMovement.width) + 1)
)) }
var body: some View {
VStack {
Text("tH \(Int(combined.height)) tW \(Int(combined.width))")
Text("tH \(Int(dragMovement.height)) tW \(Int(dragMovement.width))")
HStack {
Text("\(Int(angleFloat))º")
Slider(value: $angleFloat, in: 0...360)
}.padding()
RoundedRectangle(cornerRadius: 10)
.foregroundColor(.blue)
.frame(width: 200, height: 400, alignment: .center)
.offset(x: dragMovement.width, y: dragMovement.height)
.rotationEffect(angle, anchor: .bottom)
.scaleEffect(scale)
.gesture(
DragGesture()
.onChanged { val in
self.dragMovement = val.translation
self.gestureActive = true
}
.onEnded { val in
self.dragMovement = .zero
self.gestureActive = false
self.shouldCardBeDismissed(val.translation, val.predictedEndTranslation)
}
)
}
}
func shouldCardBeDismissed(_ translation: CGSize, _ predictedEndTranslation: CGSize) {
let combined = CGSize(width: (translation.width + predictedEndTranslation.width), height: (translation.height + predictedEndTranslation.height))
self.combined = combined
}
}
PlaygroundPage.current.setLiveView(CardSwiper())
@Viranchee
Copy link
Author

Viranchee commented Apr 23, 2020

Structure and Algorithm credits: https://github.com/NathanLawrence/SwiftRight

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