LoginSignup
8
5

More than 1 year has passed since last update.

【SwiftUI】Glassmorphismイケすぎでしょ!!

Last updated at Posted at 2022-12-17

はじめに

めっちゃかっこいいGlassmorphismをSwiftUIで再現できたので記録しておきます。
再利用できるようにButtonStyleにしてみます。

背景の画像はこちらから使用しました

サンプルアプリ

simulator_screenshot_BBF0BA45-6769-4E89-8568-C9895A5F85BB.png

実装

View

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            Image("background")
                .resizable()
                .scaledToFill()
                .edgesIgnoringSafeArea(.all)

            Button {
                print()
            } label: {
                Text("").frame(width: 320, height: 200)
            }
            .buttonStyle(.glassmorphism)
        }
    }
}

ButtonStyle

struct GlassmorphismButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .foregroundColor(.white)
            .background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 10))
            .overlay(.white.opacity(0.5), in: RoundedRectangle(cornerRadius: 10).stroke(style: .init()))
    }
}

extension ButtonStyle where Self == GlassmorphismButtonStyle {
    static var glassmorphism: GlassmorphismButtonStyle {
        .init()
    }
}

おわり

これめっちゃかっこいいです

8
5
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
8
5