Skip to content

SwiftUI variable blur (progressive blur)

License

Notifications You must be signed in to change notification settings

nikstar/VariableBlur

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VariableBlur

VariableBlur

SwiftUI variable blur (progressive blur)

First of all, all hard work was done by jtrivedi - I just made some minor adjustments.

Changes in this version:

  • all code is in one file
  • dynamically generates gradient image allowing for further adjustments
  • fixes crash when switching between light and dark mode
  • supports upside down variable blurs (clear at the top, blurred at the bottom)

Use of private API did not trigger App Store rejection for me but do tell if it does for you.

Install

Recommended

Copy VariableBlur.swift to your project.

SPM

To add a package dependency to your Xcode project, select File > Add Package and enter this repository's URL (https://github.com/nikstar/VariableBlur).

Example

Used to create image on top of this page:

ZStack(alignment: .top) {
    Color.white
    Color.blue.opacity(0.3)
    Image("im")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .padding(.horizontal, 50)
    Text("VariableBlur")
        .font(.largeTitle.monospaced().weight(.bold))
        .padding(.top, 230)
        .foregroundStyle(.white.opacity(0.9))
}
.overlay(alignment: .top) {
    VariableBlurView(maxBlurRadius: 20, direction: .blurredTopClearBottom)
        .frame(height: 200)
}
.ignoresSafeArea()

Blur matching status bar/cutout safe area:

ContentView()
    .overlay(alignment: .top) {
        GeometryReader { geom in
            VariableBlurView(maxBlurRadius: 10)
                .frame(height: geom.safeAreaInsets.top)
                .ignoresSafeArea()
        }
    }