Created
July 26, 2020 15:54
-
-
Save jhowlin/bf869decd92ad668839a77815ff5ed45 to your computer and use it in GitHub Desktop.
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
struct SwipeCell: View { | |
@State var buttonsWidth: CGFloat = 0 | |
@State var cellContentOffset: CGFloat = 0 | |
@GestureState var dragState:DragGesture.Value? = nil | |
var body: some View { | |
GeometryReader { geometry in | |
HStack { | |
Rectangle().frame(height:120).foregroundColor(.purple).cornerRadius(7) | |
.gesture(DragGesture().updating($dragState) { dragValue, dragState, _ in | |
dragState = dragValue | |
}.onChanged { value in | |
let distance = value.translation.width | |
let midPoint = geometry.size.width * 0.5 | |
let isOpening = distance < 0 ? true : false | |
if isOpening { | |
buttonsWidth = min(abs(distance),midPoint) | |
cellContentOffset = -pow(buttonsWidth, 0.5) | |
buttonsWidth += pow(buttonsWidth, 0.6) | |
} else { | |
buttonsWidth = min(distance, 0) | |
} | |
}.onEnded { _ in | |
if buttonsWidth > (geometry.size.width * 0.25) { | |
buttonsWidth = geometry.size.width * 0.5 | |
} else { | |
buttonsWidth = 0 | |
} | |
cellContentOffset = 0 | |
}).overlay(Text("Swipe Cell").offset(x: cellContentOffset, y: 0)) | |
HStack(spacing:0) { | |
Rectangle().foregroundColor(.blue).overlay(Image(systemName: "trash").foregroundColor(.white)).onTapGesture { | |
buttonsWidth = 0 | |
} | |
Rectangle().frame(width: 5).foregroundColor(.white) | |
Rectangle().foregroundColor(.red).overlay(Image(systemName: "heart").foregroundColor(.white)).onTapGesture { | |
buttonsWidth = 0 | |
} | |
}.frame(width:buttonsWidth, height:120).cornerRadius(7) | |
}.padding() | |
}.animation(.spring()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm sure can be simplified. I added a bit of a rubber banding once you cross the midpoint on opening, and slightly offset the text in the cell.
Next I'll mask the rectangle so the text is clipped correctly...