Skip to content

Instantly share code, notes, and snippets.

@op183
Last active February 7, 2020 17:51
Show Gist options
  • Save op183/208d86f213e25c4112bd0884b18878dc to your computer and use it in GitHub Desktop.
Save op183/208d86f213e25c4112bd0884b18878dc to your computer and use it in GitHub Desktop.
context menu
import SwiftUI
struct FullWidthImageView: View {
@ObservedObject var model = modelStore
var body: some View {
VStack {
Image(systemName: model.img)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 200)
Text(model.img)
}.contextMenu(ContextMenu() {
Button(action: {
self.model.toggle.toggle()
}) {
HStack {
Text("toggle image to?")
Image(systemName: model.img2)
}
}
Button("No") {}
})
}
}
class Model:ObservableObject {
@Published var toggle = false
var img: String {
toggle ? "pencil.and.outline": "trash"
}
var img2: String {
toggle ? "trash" : "pencil.and.outline"
}
}
let modelStore = Model()
struct ContentView: View {
var body: some View {
VStack {
FullWidthImageView()
Text("Long press the image to change it").bold()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment