SwiftUI - Effects



Effects are known as visual modifications or transformations applied to the view to customize the appearance or behavior of the view. They only enhance the visual appearance of the view, not impact the layout because they are not allowed to modify the layout of the view.

Using these effects we can apply various effects like shadow, blurs, rotation, saturation, hue, etc to any view or component such as Text, Image, Shape, Button, etc. They enhance the user experience by improving the readability of the content or can create interactive components.

Applying Effects in SwiftUI

SwiftUI support a wide range of pre-defined modifiers such as shadow, hover, blur, etc, that can use to enhance the appearance of the view. So simply by using these modifiers, we can apply effects on the views. We can use these modifiers directly with the help of the dot operator, for example −

Text("TutorialsPoint").blur(radius:12)

Example 1

The following SwiftUI program is used to apply the blur effect on the text view.

import SwiftUI

struct ContentView: View {
   var body: some View {
      VStack {            
         // Without any effect
         Text("TutorialsPoint").font(.largeTitle).foregroundStyle(.green)
            
         // With blur effect
         Text("TutorialsPoint")
            .font(.largeTitle)
            .foregroundStyle(.green)
            .blur(radius: 4)
      }.padding()
   }
}
#Preview {
   ContentView()
}

Output

Effects

Example 2

The following SwiftUI program is used to apply multiple effects on the text view.

import SwiftUI

struct ContentView: View {
   var body: some View {
      VStack {
         // Without any effect
         Text("TutorialsPoint").font(.largeTitle).foregroundStyle(.green)
            
         // With multiple effects
         Text("TutorialsPoint")
            .foregroundStyle(.green)
            .padding(20)
            .shadow(radius: 5)
            .rotationEffect(.degrees(10))
            .scaleEffect(2.3)
      }.padding()
   }
}
#Preview {
    ContentView()
}

Output

Effects

Types of Effects in SwiftUI

SwiftUI supports the following types of effect−

S.No Effect Description
1 scaleEffect() It is used to increase or decrease the size(horizontally and vertically) of the view.
2 rotationEffect() It is used to rotate the view around the given point in a two-dimensional plane.
3 tansformEffect() It is used to apply an affine transformation to the given view. Affine transformation includes rotation, scaling, translation or skew of the view.
4 blur() It is used to apply Gaussian blur to the specified view.
5 shadow() It is used to apply a shadow effect on the specified view.
6 blendMode() It is used to blend two overlapping views.
7 hoverEffect() It is used to apply hover effect on the specified view.
8 colorEffect() It is used to apply a filter effect on each pixel of the color.
9 distortionEffect() It is used to apply geometric distortion effect on each pixel of the given view.
10 layerEffect() It is used to apply a combination of filters on the raster layer of the view.
11 visualEffect() It is used to apply animatable visual effects by accessing the layout details of the view using a geometry proxy.
12 adjustColorEffect() It is used to apply basic color effects to the view such as saturation, brightness, contrast, hueRotation, and colorMultiply.
Advertisements