Instance Method インスタンスメソッド

onTapGesture(count:perform:)

Adds an action to perform when this view recognizes a tap gesture. このビューがタップジェスチャを認識する時に実行するアクションを加えます。

Declaration 宣言

func onTapGesture(count: Int = 1, perform action: @escaping () -> Void) -> some View

Parameters パラメータ

count

The number of taps or clicks required to trigger the action closure provided in action. Defaults to 1.

action

The action to perform. 実行するアクション。

Discussion 議論

Use this method to perform a specific action when the user clicks or taps on the view or container count times.

In the example below, the color of the heart images changes to a random color from the colors array whenever the user clicks or taps on the view twice:


struct TapGestureExample: View {
    let colors: [Color] = [.gray, .red, .orange, .yellow,
                           .green, .blue, .purple, .pink]
    @State private var fgColor: Color = .gray


    var body: some View {
        Image(systemName: "heart.fill")
            .resizable()
            .frame(width: 200, height: 200)
            .foregroundColor(fgColor)
            .onTapGesture(count: 2) {
                fgColor = colors.randomElement()!
            }
    }
}

A screenshot of a view of a heart.

See Also 参照

Taps and Gestures