Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Created July 24, 2020 07:55
Show Gist options
  • Save ozgurshn/92c03bb8b3514007b8083df420e572f4 to your computer and use it in GitHub Desktop.
Save ozgurshn/92c03bb8b3514007b8083df420e572f4 to your computer and use it in GitHub Desktop.
SwiftUI Drag with no returning to original position
//
// TestView.swift
// PhotoText
//
// Created by ozgur on 7/24/20.
// Copyright © 2020 com.ozgur. All rights reserved.
//
import SwiftUI
struct TestView: View {
@State var offset = CGSize.zero
@State private var currentPosition: CGSize = .zero
@State private var newPosition: CGSize = .zero
var body: some View {
ZStack{
Image("beach").resizable()
.offset(offset)
Image("overlay").resizable() .gesture(
DragGesture()
.onChanged { value in
self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
self.offset = self.currentPosition
}
.onEnded { value in
self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
self.newPosition = self.currentPosition
}
)
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment