2
1

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 3 years have passed since last update.

【SwiftUI】Color.clear / opacity(0)のビューのジェスチャーが反応しない問題

Posted at

#課題
ビューやパスに対して Color.clear や opacity(0) を設定した場合
タッチジェスチャーなどが反応しなくなります。

Rectangle()
    .fill(Color.clear) // or .fill(Color.red.opacity(0))
    .frame(width: 100, height: 100)
    .onTapGesture {
        print("On tap") // 呼ばれない
    }

#解決法

「.contentShape(Rectangle())」 を追加

Rectangle()
    .fill(Color.clear) // or .fill(Color.red.opacity(0))
    .frame(width: 100, height: 100)
    .contentShape(Rectangle())
    .onTapGesture {
        print("On tap") // 呼ばれる
    }

###VStack / HStack / ZStack に関しても同様

ZStack{}
    .frame(width: 100, height: 100)
    .contentShape(Rectangle())
    .onTapGesture {
        print("On tap") // 呼ばれる
    }
2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?