Created
July 13, 2020 09:48
-
-
Save yuraist/a89ad4f6cd92439ee29d5d2e7a9919c8 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 | |
// MARK: - Root view | |
struct SheetPresenterView: View { | |
@State private var isPresented = false | |
var body: some View { | |
Button(action: showSheet) { | |
Image(systemName: "plus") | |
.font(.largeTitle) | |
} | |
.sheet(isPresented: $isPresented) { | |
DragScrollView() | |
} | |
} | |
private func showSheet() { | |
isPresented = true | |
} | |
} | |
// MARK: - View containing ScrollView presented as a sheet modal | |
struct DragScrollView: View { | |
var body: some View { | |
VStack(alignment: .leading) { | |
Text("Navigation Bar") | |
.font(.largeTitle) | |
.fontWeight(.bold) | |
.padding([.top, .horizontal]) | |
ScrollView { | |
ForEach(0..<10, id: \.self) { index in | |
ListItemView() | |
} | |
} | |
.padding(.bottom) | |
} | |
} | |
} | |
// MARK: - Item of the scroll view containing the drag gesture | |
struct ListItemView: View { | |
@State private var cardXTranslation: CGFloat = 0 | |
var body: some View { | |
HStack { | |
Text("Folder") | |
.font(.headline) | |
Spacer() | |
Image(systemName: "folder") | |
} | |
.padding() | |
.background( | |
RoundedRectangle(cornerRadius: 10) | |
.fill(Color(.systemGray5)) | |
) | |
.offset(x: self.cardXTranslation) | |
.rotationEffect(.degrees(Double(self.cardXTranslation) / 50)) | |
.animation(.spring()) | |
.simultaneousGesture( | |
DragGesture(minimumDistance: 30, coordinateSpace: .local) | |
.onChanged({ gesture in | |
if abs(gesture.translation.width) < 100 { | |
self.cardXTranslation = gesture.translation.width | |
} | |
}) | |
.onEnded({ gesture in | |
self.cardXTranslation = 0 | |
}) | |
) | |
.padding(.horizontal) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment