Autumn Sale 🔥

14/ 100

promotion ends on September 20th

Mega Bundle 90% OFF

Get developer licenses for all our products at 90% off discount.

$7,613

$599

  • 15+ fully functional mobile apps
  • 600+ screens
  • 1,000+ features
  • 200,000+ lines of code
  • 1000+ paid customers
  • free lifetime updates
BUY NOW

As we have mentioned in previous posts, there are more and more iOS applications in App Store and obviously we are living in a highly competitive market. Hence, creativity and differentiation are essential for success. In today’s article, we will show you a great feature that makes apps feel more playful and performant – haptic feedback. We’ll learn how to generate haptics feedback in Swift with code examples.

1. What is Haptic Feedback?

First, let’s see how Apple explained:

Haptics engage people’s sense of touch to enhance the experience of interacting with onscreen interfaces. For example, when an Apple Pay transaction is confirmed, the system plays haptics in addition to providing visual and auditory feedback. Haptics can also enhance touch gestures and interactions like scrolling through a picker or toggling a switch.

Well, we think this is quite clear but in case, you’re still confused, then here you are. The haptic feedback feature helps us attract users by tactile feedback. This is also a cool feature to help users guess the result of any action without actually watching the screen.

2. How to Implement Haptic Feedback in Swift?

The first thing we need to know is UIFeedbackGenerator. This is the abstract superclass for all feedback generators. And as a habit, we would think: “Oh okay, we’ll create subclasses to use”. However, this is not Apple’s advice since they have provided us with the following three classes:

- UIImpactFeedbackGenerator

- UISelectionFeedbackGenerator

- UINotificationFeedbackGenerator

So, instead of defining our custom vibration patterns, we are going to use predefined haptic feedback patterns from the system. Now, let’s discover these three classes:

  • UIImpactFeedbackGenerator. This generator is suitable for notifying that there is an impact has occurred. It has three levels: .light, .medium, and .heavy.
  • UISelectionFeedbackGenerator: This generator is used in order to indicate a change in selections.
  • UINotificationFeedbackGenerator. The name says at all, use this generator for notification, and it has three types:.success, .warning, and .error.

3. How to Generate Feedback in iOS Apps

There really isn’t any best way – it depends on the style and approach of each project to have the right pattern. For us, we found that we needed a function to convert the user’s wishes into the corresponding signal. Hence, using a static func with a helper class is pretty straightforward. Let’s get one of them as an example: UIImpactFeedbackGenerator

let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .light)
impactFeedbackgenerator.prepare()
impactFeedbackgenerator.impactOccurred()
impactFeedbackgenerator = nil

For this, we will have some styles

public enum FeedbackStyle : Int {
  case light
  case medium
  case heavy
}

You can use the same pattern with the rest and then put it into the static func.

Conclusion

So, we have introduced a pretty cool feature to make your iOs app more stand out. Technically, it’s quite simple, the difference is in how you choose the type of haptic for each particular action that provides for the best user experience. Thank you for reading this article and feel free to share it if you like.

Categories: iOS Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping Cart