-
-
Save sturdysturge/3f9e7a301cb045383629f77b360a74ed 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
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