2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】長押しでメニューを表示する

Posted at

はじめに

ContextMenuは長押しした時に出るメニューです。
意外と知られてなさそうなので紹介します。

動画

Simulator Screen Recording - iPhone 14 - 2022-11-20 at 21.50.34.gif

データ

struct Weather: Identifiable {
    let id = UUID()
    let iconName: String
    let iconColor: Color
    let title: String
}

let weathers: [Weather] = [
    .init(iconName: "sun.max.fill", iconColor: .orange, title: "晴れ"),
    .init(iconName: "cloud.fill", iconColor: .gray, title: "曇り"),
    .init(iconName: "cloud.rain.fill", iconColor: .blue, title: "雨")
]

実装

import SwiftUI

struct ContentView: View {
    var body: some View {
        List(weathers) { weather in
            Label {
                Text(weather.title)
            } icon: {
                Image(systemName: weather.iconName)
                    .foregroundColor(weather.iconColor)
            }
+           .contextMenu {
+               Button {
+                   print()
+               } label: {
+                   Text("テスト")
+               }
+           }
        }
        .listStyle(.grouped)
    }
}

おわり

かなりかっこいいので積極的に使っていきたいです

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?