SwiftGlass

repository·main·Indexed 18 days ago

https://github.com/1998code/swiftglass

A cross-platform SwiftUI library for creating customizable glassmorphism effects across iOS, macOS, watchOS, tvOS, and visionOS. It provides a `.glass()` ViewModifier to apply default or fine-tuned glass effects, supporting various shapes (circle, capsule, rounded rectangle) and parameters for radius, color, material, and shadows. The library utilizes native APIs like `.glassBackgroundEffect()` on visionOS and custom implementations on other platforms.

Tokens
3.5K
Snippets
12
Records
16
Agent score
58%

What's inside SwiftGlass

  1. Platform-specific behavior for SwiftGlass

    main

    SwiftGlass adapts its implementation based on the target platform:

    • visionOS: Uses the native .glassBackgroundEffect() to ensure optimal rendering and system integration.
    • Other Platforms (iOS, macOS, watchOS, tvOS, iPadOS): Achieves a similar effect through a custom implementation combining materials, gradients, and shadows.
  2. Platform-specific behavior of SwiftGlass

    main

    SwiftGlass adapts its implementation based on the target platform:

    • visionOS: Uses the native .glassBackgroundEffect() for optimal rendering and system integration.
    • iOS 26+ / macOS 26+: Uses the native .glassEffect() API with enhanced toolbar support.
    • Other platforms (iOS, macOS, watchOS, tvOS, iPadOS): Uses a custom implementation combining materials, gradients, and shadows to achieve a similar effect.
    • Colors: Shadow and base colors automatically adapt to system background colors on each platform.
  3. Install SwiftGlass via Swift Package Manager

    main

    You can add SwiftGlass to your Xcode project using Swift Package Manager (SPM).

    Using the Xcode UI:

    1. Go to your Project settings.
    2. Select Package Dependencies.
    3. Click the + button.
    4. Enter the repository URL: https://github.com/1998code/SwiftGlass.git.

    Using Swift Package Manager manifest (Package.swift): Add the following to your dependencies array:

    .package(url: "https://github.com/1998code/SwiftGlass.git", from: "1.0.0")
    dependencies: [
        .package(url: "https://github.com/1998code/SwiftGlass.git", from: "1.0.0")
    ]
  4. Customize glass effects with parameters

    main

    You can fine-tune the appearance of the glass effect by passing arguments to the .glass() modifier. This allows you to control the corner radius, base color, material type, gradient opacity, and shadow properties.

    VStack {
        Text("自訂玻璃")
        Image(systemName: "sparkles")
    }
    .padding(30)
    .glass(
        radius: 20,
        color: .blue,
        material: .regularMaterial,
        gradientOpacity: 0.7,
        shadowColor: .blue,
        shadowRadius: 10
    )
  5. Apply basic glass effect with .glass()

    main

    You can apply a default glass effect to any SwiftUI view using the .glass() view modifier. This is the simplest way to add a glass-like appearance to text, buttons, or containers.

    import SwiftUI
    import SwiftGlass
    
    struct ContentView: View {
        var body: some View {
            Text("안녕, 유리!")
                .padding(40)
                .glass() // Applies default glass effect
        }
    }
  6. Use different glass shapes

    main

    SwiftGlass supports several shapes for the glass background. You can specify the shape using the shape parameter in the .glass() modifier.

    // Circle shape
    Image(systemName: "heart.fill")
        .frame(width: 100, height: 100)
        .glass(shape: .circle, color: .pink)
    
    // Capsule shape (pill-shaped)
    Button("Play Music") { }
        .padding()
        .glass(shape: .capsule, color: .blue)
    
    // Rounded rectangle (default)
    Text("Hello")
        .padding()
        .glass(radius: 20, shape: .roundedRectangle(radius: 20))
  7. Apply basic glass effects using .glass()

    main

    SwiftGlass provides a simple .glass() ViewModifier that can be applied to any SwiftUI view (such as Text, Image, or containers like VStack) to apply a default glass effect.

    First, ensure you import the module:

    import SwiftUI
    import SwiftGlass
    import SwiftUI
    import SwiftGlass
    
    struct ContentView: View {
        var body: some View {
            Text("你好, 玻璃!")
                .padding(40)
                .glass() // Use default glass effect
        }
    }
  8. Apply the default glass effect

    main

    The simplest way to use SwiftGlass is by applying the .glass() ViewModifier to any SwiftUI view. This applies the default glass effect with standard materials and shadows.

    import SwiftUI
    import SwiftGlass
    
    struct ContentView: View {
        var body: some View {
            Text("Hello, Glass!")
                .padding(40)
                .glass() // Use default glass effect
        }
    }
  9. Apply a custom glass effect

    main

    You can fine-tune the appearance of the glass effect by passing parameters to the .glass() modifier. This allows you to control the radius, color, material, gradient opacity, and shadow properties.

    VStack {
        Text("カスタムガラス")
        Image(systemName: "sparkles")
    }
    .padding(30)
    .glass(
        radius: 20,
        color: .blue,
        material: .regularMaterial,
        gradientOpacity: 0.7,
        shadowColor: .blue,
        shadowRadius: 10
    )
  10. Apply custom glass effect with parameters

    main

    For more control over the appearance, use the .glass() modifier with specific parameters to adjust radius, color, material, gradients, and shadows.

    VStack {
        Text("커스텀 유리")
        Image(systemName: "sparkles")
    }
    .padding(30)
    .glass(
        radius: 20,
        color: .blue,
        material: .regularMaterial,
        gradientOpacity: 0.7,
        shadowColor: .blue,
        shadowRadius: 10
    )
  11. Customize the glass effect

    main

    You can fine-tune the appearance of the glass effect by passing parameters to the .glass() modifier. This allows you to control the radius, color, material, gradient opacity, and shadow properties.

    VStack {
        Text("Custom Glass")
        Image(systemName: "sparkles")
    }
    .padding(30)
    .glass(
        radius: 20,
        color: .blue,
        material: .regularMaterial,
        gradientOpacity: 0.7,
        shadowColor: .blue,
        shadowRadius: 10
    )
  12. Apply basic glass effects with .glass()

    main

    SwiftGlass provides a simple ViewModifier called .glass() that can be applied to any SwiftUI view (such as Text, Button, or containers) to apply a default glass effect.

    Ensure you import SwiftGlass in your file.

    import SwiftUI
    import SwiftGlass
    
    struct ContentView: View {
        var body: some View {
            Text("Hello, Glass!")
                .padding(40)
                .glass() // Applies the default glass effect
        }
    }