Skip to content

Instantly share code, notes, and snippets.

@jayesh15111988
Last active August 29, 2022 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayesh15111988/3f6e855e47b4d9945aaebc997baca8a5 to your computer and use it in GitHub Desktop.
Save jayesh15111988/3f6e855e47b4d9945aaebc997baca8a5 to your computer and use it in GitHub Desktop.
A Gist to demo SwiftUI's swipe actions modifier in action
import SwiftUI
struct Place: Identifiable {
let id: String
let name: String
let imageName: String
var isFavorited: Bool = false
}
struct SwipeActionsModifier: View {
@State var places = [
Place(id: "1", name: "Mumbai", imageName: "car"),
Place(id: "2", name: "Vienna", imageName: "airplane"),
Place(id: "3", name: "Amsterdam", imageName: "camera"),
Place(id: "4", name: "Prague", imageName: "alarm"),
Place(id: "5", name: "Delhi", imageName: "bag")
]
var body: some View {
List {
ForEach($places) { $place in
HStack {
let tintColor: Color = place.isFavorited ? .pink : .black
Image(systemName: place.imageName).foregroundColor(tintColor)
Text(place.name).foregroundColor(tintColor)
}.swipeActions(edge: .leading) {
Button {
withAnimation {
place.isFavorited.toggle()
}
} label: {
if place.isFavorited {
Label("Unfavorite", systemImage: "heart.slash")
} else {
Label("Favorite", systemImage: "heart")
}
}
}.swipeActions(edge: .leading) {
Button {
// no-op
} label: {
Label("Pin", systemImage: "pin")
}
}.swipeActions(edge: .trailing) {
Button {
// no-op
} label: {
Label("Cut", systemImage: "scissors")
}
}.swipeActions(edge: .trailing) {
Button {
// no-op
} label: {
Label("Focus", systemImage: "scope")
}
}
}
}
}
}
struct SwipeActionsModifier_Previews: PreviewProvider {
static var previews: some View {
SwipeActionsModifier()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment