Glur

1.0.3

Progressive blurs in SwiftUI.
joogps/Glur

What's New

v1.0.3

2024-02-20T05:20:06Z

🛠️ Fix radius mapping for up and left directions

Glur Project logo


Twitter: @joogps

A SwiftUI library that uses Metal to display efficient progressive blurs, just like the ones used by Apple.

Installation

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.

Usage

You can add a glur effect with the following modifier:

.glur()

Here are all optional parameters:

.glur(radius: 8.0, // The full radius of the blur once it has interpolated
      offset: 0.3, // The offset until the blur starts being applied, relative to the size of the view
      interpolation: 0.4, // The interpolation until the blur reaches its full radius, relative to the size of the view
      direction: .down // The direction of the blur
)

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.

How it's done

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.

Demo

You can run a demo of Glur in your device or simulator through the GlurDemo project in this repository.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Mar 02 2024 12:46:06 GMT-1000 (Hawaii-Aleutian Standard Time)