Created
July 24, 2020 07:55
-
-
Save ozgurshn/92c03bb8b3514007b8083df420e572f4 to your computer and use it in GitHub Desktop.
SwiftUI Drag with no returning to original position
This file contains hidden or 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
// | |
// 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