A Gist to demo SwiftUI's swipe actions modifier in action
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 | |
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