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

【SwiftUI】輝度が低い色ほど透明度を上げる

Posted at

はじめに

luminanceToAlphaという機能を発見して、使ったことがなかったので調べてみました。
簡単にいうと、Viewの色が明るければ明るいほど、Viewが透明になるよって機能です。

iOS13から使えるようですが、あまり登場シーンがないので存在を知りませんでした。

サンプルアプリ

Simulator Screen Recording - iPhone 15 - 2023-12-23 at 22.42.10.gif

実装

import SwiftUI

struct ContentView: View {
    @State private var selectedColor = Color.black

    var body: some View {
        VStack {
            Rectangle()
                .frame(height: 50)
                .foregroundStyle(selectedColor)
                .border(.black)
                .background {
                    Text("これが見えたら透過してるよ").foregroundStyle(selectedColor)
                }
            
            Rectangle()
                .frame(height: 50)
                .foregroundStyle(selectedColor)
                .luminanceToAlpha()
                .border(.black)
                .background {
                    Text("これが見えたら透過してるよ").foregroundStyle(selectedColor)
                }
            
            ColorPicker("", selection: $selectedColor)
        }
        .padding(20)
    }
}

活用事例を考えてみる

使われないのは可哀想なので活用できる場面を考えてみる

...

思いつかない

おわり

GitHubでどんな使われ方をしているのか見てみたらmaskと一緒に使われているのが多かった気がします。

公式ドキュメント

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