SwiftUI - Blur Effect



The Blur effect in SwiftUI applies a blurring filter on the user interface's specified view, background, or image. It is commonly used to enhance the visual experience, creating depth, focusing on the specified components or improve the app's user experience.

SwiftUI supported the Gaussian blur filter, which is a blurring type of filter which uses a mathematical function known as the Gaussian function. It creates a blur effect by smoothing the image by averaging the pixels in the given radius. In SwiftUI, we can achieve a blur effect using the following ways −

  • blur() Modifier
  • Material Type

The blur() Modifier

SwiftUI provides an in-built modifier named blur() to achieve a blur effect. This modifier allows us to add a custom Gaussian blur effect to any view instantly. It is commonly used to blur the background to enhance the foreground components.

Syntax

Following is the syntax −

func blur(radius: CGFloat, opaque: Bool = false)->some View

Parameters

This modifier takes the following parameters −

  • radius: It represents the radial size of the blur.
  • opaque: If the value of this parameter is true, then we are allowed to create an opaque blur. Otherwise, we are only allowed for transparency. It is an optional parameter.

Example

The following SwiftUI program is used to blur an image view.

import SwiftUI

struct ContentView: View {
   var body: some View {
      VStack{
         Text("Original Image:").font(.largeTitle)
         Image("wallpaper").resizable()
            .frame(width: 300, height: 300)
         Text("Blurred Image:").font(.largeTitle)
         Image("wallpaper")
            .resizable()
            .frame(width: 300, height: 300)
            .blur(radius: 4.5)
      }
   }
}

#Preview {
   ContentView()
}

Output

Blur Effect

Material Type

Apart from the blur() modifier SwiftUI provides us with another method to create a blur effect that is Material type. The Material type is a collection of pre-defined blur effects with different levels of translucency effect. Using these we can achieve a feeling of depth.

They allow us to blur the background without affecting the foreground elements. They are not view but they will create a translucent layer between the view and its background. They are defined in Human InterfaceGuidelines. Material types are commonly used with background () or overlay() modifiers.

SwiftUI supports the following type of Material types −

  • .regularMaterial: It is used to apply the regular material effect.
  • .thinMaterial: It is used to apply the lighter material effect.
  • .ultraThinMaterial: It is used to apply the lightest material effect.
  • .thickMaterial: It is used to apply a thicker material effect.
  • .ultraThickMaterial: It is used to apply a thickest material effect.
  • .bar: It is used to apply suitable material effects for bars and toolbars.

Syntax

Following is the syntax −

.backgound(.thinMaterial)

Example

The Following SwiftUI program is used to blur the current view using Material Type.

import SwiftUI

struct ContentView: View {
   var body: some View {
      ZStack {
         Rectangle().fill(.mint)
         VStack{
            Text("Thin Material")
            Rectangle()
               .frame(width: 250, height: 100)
               .background(.thinMaterial)
            Text("Thick Material")
            Rectangle()
               .frame(width: 250, height: 100)
               .background(.thickMaterial)
            
            Text("Ultra Thin Material")
            Rectangle()
               .frame(width: 250, height: 100)
               .background(.ultraThinMaterial)
            
            Text("Thick Material")
            Rectangle()
               .frame(width: 250, height: 100)
               .background(.ultraThickMaterial)
            
            Text("Regular Material")
            Rectangle()
               .frame(width: 250, height: 100)
               .background(.regularMaterial)
         }
      }      
   }
}
#Preview {
   ContentView()
}

Output

Blur Effect
Advertisements