Wesley de Groot's Blog
Sensory feedback in SwiftUI

Back

This post explores the concept of sensory feedback in SwiftUI, a modifier to provide feedback to users based on their interactions.

What is Sensory Feedback in SwiftUI?

Sensory Feedback in SwiftUI is a powerful feature that allows developers to create responsive and interactive user interfaces. It enables the triggering of actions and updates in the UI based on user interactions or changes in data.

Sensory feedback can include haptic feedback, sound effects, and visual cues that enhance the user experience by providing immediate and relevant responses to user actions.

What are the options

.sensoryfeedback(_:trigger:) allows you to specify the type of feedback and the trigger for that feedback.

  • .success: Indicates that a task or action has completed.

  • .warning: Indicates that a task or action has produced a warning of some kind.

  • .error: Indicates that an error has occurred.

Use Case: Button Press Feedback

In this example, we provide haptic feedback when a button is pressed:

import SwiftUI

struct ContentView: View {
    @State private var action = true
    var body: some View {
        Button("Tap me") {
            action.toggle()
        }
        .sensoryFeedback(.success, trigger: action)
    }
}

Use Case: Button Press Feedback (Custom intensity)

In this example, we provide haptic feedback with custom intensity when a button is pressed:

struct ContentView: View {
    @State private var trigger = false

    var body: some View {
        NavigationStack {
            Button("Action") {
                // do something
                trigger.toggle()
            }
            .sensoryFeedback(
                .impact(weight: .heavy, intensity: 0.9),
                trigger: trigger
            )
        }
    }
}

Use Case Button Press Feedback (based on value)

In this example, we provide haptic feedback based on the value of a state variable when a button is pressed:

import SwiftUI

struct ContentView: View {
    @State private var action = true
    var body: some View {
        Button("Tap me") {
            action.toggle()
        }
        .sensoryFeedback(trigger: action) { oldValue, newValue in
            return newValue.isEmpty ? .error : .success
        }
    }
}

Caveats

While Sensory Feedback in SwiftUI can greatly enhance user interactions, it's important to use it judiciously. Overusing haptic feedback or other sensory cues can lead to a cluttered user experience. Always consider the context and the user's needs when implementing these features.

Wrap up

In summary, Sensory Feedback in SwiftUI is a valuable tool for creating responsive and interactive user interfaces. By providing immediate feedback based on user interactions, it can significantly enhance the overall user experience.

Conclusion

In conclusion, Sensory Feedback in SwiftUI is an essential aspect of modern app development. By incorporating haptic feedback, sound effects, and visual cues, developers can create more engaging and intuitive user experiences. As you explore SwiftUI, consider how you can leverage these features to improve your app's interactivity and responsiveness.

Resources:

Read more

Share


Share Bluesky Mastodon Twitter LinkedIn Facebook
x-twitter mastodon github linkedin discord threads instagram whatsapp bluesky square-rss sitemap