Skip to content

Instantly share code, notes, and snippets.

@erica
Created June 7, 2019 16:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/3d68436231219d13074b3b12d5b77068 to your computer and use it in GitHub Desktop.
Save erica/3d68436231219d13074b3b12d5b77068 to your computer and use it in GitHub Desktop.
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