Search code examples
iosmacos3dtouchforce-touch

Can I add feedback (notch) to a gesture on iOS using 3D Touch?


With Force Touch on OS X applications can provide a feedback or a notch (haptics/taptics?) like Apple's example describes:

Map rotation: You'll feel a notch when you rotate the compass to north in Maps.

Is the the same thing possible with 3D Touch beyond just the old audio API to vibrate the device (AudioServicesPlayAlertSound)?


Solution

  • Yes, using UIFeedbackGenerator in iOS 10+ on supported devices you could do something like this:

    var feedbackGenerator: UISelectionFeedbackGenerator
    
    func prepare() {
        // Instantiate a new generator.
        feedbackGenerator = UISelectionFeedbackGenerator()
    
        // Prepare the generator when the gesture begins.
        feedbackGenerator?.prepare()
    }
    
    func notch() {
        // Trigger selection feedback.
        feedbackGenerator?.selectionChanged()
    
        // Release the current generator.
        feedbackGenerator = nil
    }