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

【SwiftUI】影を特定のViewだけに付与する

Last updated at Posted at 2022-09-02

はじめに

SwiftUIを使っていてこれどうやるんだ?と思った事があったので記事にします。

現在のコード

struct ContentView: View {
    var body: some View {
        Text("影のテストです")
            .frame(width: 200, height: 80)
            .background(Color.yellow)
            .shadow(color: .black, radius: 3, x: -5, y: -5)
    }
}

問題

背景の黄色だけに影を付けたいのにTextにも影が付く
スクリーンショット 2022-09-02 22.19.23.png

解決方法

shadowの前にcompositingGroupを付ける

struct ContentView: View {
    var body: some View {
        Text("影のテストです")
            .frame(width: 200, height: 80)
            .background(Color.yellow)
+           .compositingGroup()
            .shadow(color: .black, radius: 3, x: -5, y: -5)
    }
}

スクリーンショット 2022-09-02 22.25.53.png

おまけ(なんで?)

cornerRadiusでも同じ事ができます。

struct ContentView: View {
    var body: some View {
        Text("影のテストです")
            .frame(width: 200, height: 80)
            .background(Color.yellow)
+           .cornerRadius(0)
            .shadow(color: .black, radius: 3, x: -5, y: -5)
    }
}

スクリーンショット 2022-09-02 22.25.53.png

おわり

影は貫通するんですね

3
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
3
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?