Featured image of post Haptic Feedback Generators in iOS

Haptic Feedback Generators in iOS

Haptic feedback provides a tactile response and reinforces actions and events.

Introduction

Haptic feedback is a powerful tool in iOS development that goes beyond simply grabbing the user’s attention. It enhances the user experience by providing subtle yet impactful feedback through vibrations and taps. This feedback creates a sense of immersion and reinforces user actions, making interactions with your app feel more natural and responsive.

iOS 17.0 and later

For apps targeting iOS 17.0 and above, the sensoryFeedback modifier offers a streamlined way to integrate haptics. This modifier takes two arguments: the type of feedback (e.g., .success, .warning, .error) and a trigger value.

1
2
3
4
5
6
@State private var counter = 0

Button("Tap Count: \(counter)") {
    counter += 1
}
.sensoryFeedback(.increase, trigger: counter)

Haptic Feedback Generators (Pre-iOS 17)

For apps that need to support earlier versions of iOS, Apple provides three UIFeedbackGenerator subclasses to generate haptic feedback:

  • UIImpactFeedbackGenerator: This is used to indicate an impact has occurred, like a button press or a UI element snapping into place.

  • UINotificationFeedbackGenerator: This provides feedback for tasks or actions, conveying success, warning, or error states. (e.g., login attempt or pull-to-refresh)

  • UISelectionFeedbackGenerator: Use this generator for selection changes, providing light taps while scrolling a picker or slider.

Feedback Generators Tips

  • If you want to include sound along with the haptic feedback, you need to manually play the sound and sync it with the haptics.

  • Always use feedback for its intended purpose. Don’t select a haptic because of the way it feels.

  • The source of the feedback must be clear to the user. For example, the feedback must match a visual change in the user interface, or must be in response to a user action. Feedback should never come as a surprise.

  • Don’t overuse feedback. Overuse can cause confusion and diminish the feedback’s significance.

How to use Haptic Feedback Generators?

The included HapticHelper class demonstrates a way to simplify haptic feedback usage in your code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
enum HapticHelper {

  // MARK: - Types
  enum HapticType {
    case buttonTapped
    case pickerChanged
    case success
    case warning
    case error
  }

  // MARK: - Static Methods
  static func sensoryFeedback(_ type: HapticType) {
    switch type {
    case .buttonTapped:
      let generator = UIImpactFeedbackGenerator(style: .medium)
      generator.impactOccurred()

    case .pickerChanged:
      let generator = UISelectionFeedbackGenerator()
      generator.selectionChanged()

    case .success:
      let generator = UINotificationFeedbackGenerator()
      generator.notificationOccurred(.success)

    case .warning:
      let generator = UINotificationFeedbackGenerator()
      generator.notificationOccurred(.warning)

    case .error:
      let generator = UINotificationFeedbackGenerator()
      generator.notificationOccurred(.error)
    }
  }
}

In Conclusion

By effectively using haptic feedback in your iOS app, you can create a more engaging and user-friendly experience. Remember, haptics are most impactful when used thoughtfully and strategically.

Additional Notes:

  • Haptic capabilities might vary depending on the specific iOS device model.
  • Consider including a toggle in your app settings to allow users to enable or disable haptic feedback based on their preference.

Preferences

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy