A SwiftUI library that uses Metal to display efficient progressive blurs, just like the ones used by Apple. Try it today on the App Store.
![A comparison of Glur and a simple masked material](https://private-user-images.githubusercontent.com/41346220/311547509-1c748b09-a8e4-4782-a250-8563f106f298.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUwNzIxNzIsIm5iZiI6MTcyNTA3MTg3MiwicGF0aCI6Ii80MTM0NjIyMC8zMTE1NDc1MDktMWM3NDhiMDktYThlNC00NzgyLWEyNTAtODU2M2YxMDZmMjk4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MzElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODMxVDAyMzc1MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY1MjFiODBhOGIyNDYwNmQwM2Q1YTRlODk2ZGJlMGI1ZDg5ZDcyMmQ3MGU3Y2UzY2ZhZWQwOGQ3YzFiYzNkNTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.j3J0PDvDVVx-i7uiAHC35RcltJcioqF4qecisICNswc)
This repository is a Swift package, so just include it in your Xcode project and target under File > Add package dependencies. Then, import Glur
to the Swift files where you'll be using it.
Note
While Glur is supported on older platforms, it will only utilize the Metal implementation of the blur effect on iOS 17.0 and later, macOS 14.0 and later, and tvOS 17.0 and later. Otherwise, it will present a worse, compatibility effect that should be tested by the developer before being used in production.
The Metal implementation is not available on watchOS, and therefore the compatibility effect will be presented on this platform by default.
You can add a glur effect with the following modifier:
.glur()
Here are all optional parameters:
.glur(radius: 8.0, // The total radius of the blur effect when fully applied.
offset: 0.3, // The distance from the view's edge to where the effect begins, relative to the view's size.
interpolation: 0.4, // The distance from the offset to where the effect is fully applied, relative to the view's size.
direction: .down // The direction in which the effect is applied.
)
Warning
When being used in the iOS simulator, SwiftUI shader effects may not be displayed if the view exceeds 545 points in either dimension. Please note that, on a physical device, the effect should work as intented.
This project builds on a proof of concept developed in June of 2023, right after WWDC.
It makes use of Apple's new simplified Shader API for SwiftUI. First, I coded a Metal shader that produced a gaussian blur for the modified view with the correct gaussian weights distribution, efficiently. Then, I modified it slightly to vary the blur radius over the vertical or horizontal axis given the offset, interpolation and direction values.
Warning
Given that the shader is applied through Apple's own Shader API for SwiftUI, it is restricted by the limitations imposed by that API. This means that Glur can only be applied to pure SwiftUI views, excluding UIKit-backed views, such as ScrollView
.
Tip
If you want to learn how to write your first Metal shader with SwiftUI, check out this tutorial that I wrote for the Cindori blog.
You can run a demo of Glur in your device or simulator through the GlurDemo project in this repository.