0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

立体

0
Posted at

//
// NeumorphicInsetShape.swift
// MoneyManagementApp
//
// Created by Engineer MacBook Air on 2026/11/16.
//

import SwiftUI

// MARK: - 色
extension Color {
/// E8EAE9(タイルと背景に使う色。背景と同じ色にするのがニューモーフィズムの条件)
static let neumorphBase = Color(red: 232 / 255, green: 234 / 255, blue: 233 / 255)
/// 0D2750(影の色)
static let neumorphInk = Color(red: 13 / 255, green: 39 / 255, blue: 80 / 255)
}

// MARK: - 凹んだニューモーフィズムの図形
/// 「Shadow 1(ドロップシャドウ2つ)+ インナーシャドウ2つ」の凹んだタイル。
///
/// デザインの数値は 476pt 角のタイルが基準。小さい図形に使うときは scale を下げ、
/// cornerRadius もそれに合わせる(例: 300pt 角なら scale 0.63、cornerRadius 82)。
/// Figmaのブラー値は、SwiftUIでは半分の値(radius = blur / 2)を使う。
struct NeumorphicInsetShape: View {
var cornerRadius: CGFloat = 130
var scale: CGFloat = 1

private var shape: RoundedRectangle {
    RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
}

var body: some View {
    ZStack {
        // 本体 + ドロップシャドウ2つ
        shape
            .fill(Color.neumorphBase)
            // 左上の白い光: X -31 / Y -31 / Blur 43 / FFFFFF 100%
            .shadow(color: .white, radius: 43 / 2 * scale, x: -31 * scale, y: -31 * scale)
            // 右下の淡い影: X 8 / Y 8 / Blur 10 / 0D2750 4%
            .shadow(color: Color.neumorphInk.opacity(0.04), radius: 10 / 2 * scale, x: 8 * scale, y: 8 * scale)

        // インナーシャドウ1: X -33 / Y -33 / Blur 46 / FFFFFF 64%(右下の内側に出る白いハイライト)
        InnerShadow(
            shape: shape, color: .white.opacity(0.64),
            blur: 46 * scale, x: -33 * scale, y: -33 * scale
        )

        // インナーシャドウ2: X 28 / Y 28 / Blur 54 / 0D2750 16%(左上の内側に出る影)
        InnerShadow(
            shape: shape, color: Color.neumorphInk.opacity(0.16),
            blur: 54 * scale, x: 28 * scale, y: 28 * scale
        )
    }
}

}

// MARK: - インナーシャドウ
/// 図形から、(x, y) ずらした同じ図形を切り抜いた部分をぼかして、図形の内側にだけ表示する。
private struct InnerShadow: View {
let shape: S
let color: Color
/// Figmaのブラー値(内部で半分にしてSwiftUIのblurに渡す)
let blur: CGFloat
let x: CGFloat
let y: CGFloat

var body: some View {
    shape
        .fill(color)
        .mask {
            // 全面から、ずらした図形の部分を抜く
            Rectangle()
                .overlay {
                    shape
                        .offset(x: x, y: y)
                        .blendMode(.destinationOut)
                }
                .compositingGroup()
        }
        .blur(radius: blur / 2)
        .mask(shape)    // ぼかしが図形の外にはみ出さないようにする
}

}

// MARK: - 背景として使う場合
extension View {
/// 凹んだニューモーフィズムを背景に付ける
func neumorphicInset(cornerRadius: CGFloat = 130, scale: CGFloat = 1) -> some View {
background {
NeumorphicInsetShape(cornerRadius: cornerRadius, scale: scale)
}
}
}

// MARK: - Preview
#Preview {
ZStack {
Color.neumorphBase.ignoresSafeArea()

    NeumorphicInsetShape(cornerRadius: 82, scale: 0.63)
        .frame(width: 300, height: 300)
}

}

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?