SwiftUIでImageを長押しするとContextMenuを表示する
SwiftUIでImageを長押しするとContextMenuを表示する方法です。 アイコンが表示されているImageを長押しするとContextMenuが表示されます。 少しのコードを書くだけでリッチなUIが実現できて便利です。
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
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
Image("icon") | |
.resizable() | |
.frame(width: 200, height: 200) | |
.contextMenu { | |
Button(action: { | |
print("fight") | |
}) { | |
Text("FIGHT") | |
Image(systemName: "figure.wave") | |
} | |
Button(action: { | |
print("bag") | |
}) { | |
Text("BAG") | |
Image(systemName: "bag") | |
} | |
Button(action: { | |
print("pokemon") | |
}) { | |
Text("POKEMON") | |
Image(systemName: "hare") | |
} | |
Button(action: { | |
print("run") | |
}) { | |
Text("RUN") | |
Image(systemName: "figure.walk") | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |