Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created March 14, 2023 02:41
Show Gist options
  • Save sturdysturge/3f9e7a301cb045383629f77b360a74ed to your computer and use it in GitHub Desktop.
Save sturdysturge/3f9e7a301cb045383629f77b360a74ed to your computer and use it in GitHub Desktop.
import SwiftUI
@available(iOS 16.0, *)
struct ContentView {
@GestureState private var dragOffset = CGSize.zero
@State private var position = CGSize.zero
@State private var endSpinRotation = Angle.zero
var draggingRotation: Angle {
.degrees(CGPoint.zero.angle(to: CGPoint(dragOffset)))
}
func difference(_ positionLength: CGFloat,
_ frameLength: CGFloat) -> CGFloat {
let pastMiddle = positionLength > (frameLength / 2)
let adjustedLength = frameLength + 50
return pastMiddle ? adjustedLength : -adjustedLength
}
func dragEnded(_ translation: CGSize, _ frame: CGRect) {
// Start at drag location to prevent
// automatic reset to start location
self.position.height += translation.height
self.position.width += translation.width
self.endSpinRotation = draggingRotation
withAnimation {
do {
let heightDifference = difference(self.position.height, frame.height)
self.position.height = translation.height + heightDifference
}
do {
let widthDifference = difference(self.position.width, frame.width)
self.position.width = translation.width + widthDifference
}
self.endSpinRotation = .degrees(360)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment