Created
April 23, 2020 19:35
-
-
Save Viranchee/63ae364ade6d161ca7f7e308b4a14646 to your computer and use it in GitHub Desktop.
SwiftBounty
This file contains hidden or 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
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Structure and Algorithm credits: https://github.com/NathanLawrence/SwiftRight