import PlaygroundSupport | |
import SwiftUI | |
extension View { | |
public func offset(by offset: CGPoint) -> Self.Modified<_OffsetEffect> { | |
self.offset(x: offset.x, y: offset.y) | |
} | |
} | |
struct LiveView : View { | |
@State var position: CGPoint = .zero | |
var body: some View { | |
ZStack { | |
Circle() | |
.foregroundColor(Color.black.opacity(0.1)) | |
.frame(width: 320, height: 320) | |
Image(systemName:"asterisk.circle.fill") | |
.scaleEffect(CGSize(width: 3, height: 3)) | |
.foregroundColor(Color.blue.opacity(0.5)) | |
.offset(by: self.position) | |
.animation(.spring()) | |
.gesture(DragGesture() | |
.onChanged { | |
self.position = $0.location | |
} | |
.onEnded { _ in | |
if sqrt(self.position.x * self.position.x + self.position.y * self.position.y) > 160 { | |
self.position = CGPoint.zero | |
} | |
} | |
) | |
} | |
} | |
} | |
PlaygroundPage.current.liveView = UIHostingController(rootView: LiveView()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment