We like receive a physical, visual or sound response when we click on the button or other UI elements.
To help developers Apple created few classes which handle vibration effect for user’s action. These classes are UISelectionFeedbackGenerator, UIImpactFeedbackGenerator, and UINotificationFeedbackGenerator.
Note: devices older than iPhone 7 ignore haptic feedback so it might don’t work on your device.
They are very easy to use: instantiate, prepare and get feedback.
UISelectionFeedbackGenerator is intended to use when the user changes something in selection. It has only one mode.
let feedbackGenerator : UISelectionFeedbackGenerator = UISelectionFeedbackGenerator()
feedbackGenerator.prepare()
feedbackGenerator.selectionChanged()
UIImpactFeedbackGenerator is intended to use when some impact has happened. It has three modes: heavy, medium and light
let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .heavy)
impactFeedbackgenerator.prepare()
impactFeedbackgenerator.impactOccurred()
UINotificationFeedbackGenerator is intended to use when something succeeds, warned and failed. It also has three modes success, warning, and failure.
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
notificationFeedbackGenerator.prepare()
notificationFeedbackGenerator.notificationOccurred(.success)
Prepare each FeedbackGenerator before usage to reduce delay, such delay may confuse user.