On UIButton action the Taptic of device is not working

854 Views Asked by At

There is a taptic which on long press user get vibrate felling with device for a second.

My device is iPhone 6s version 12.6.1 hope it support the taptic.

@IBAction func onClick(_ sender: Any) {
    let generator = UIImpactFeedbackGenerator(style: .heavy)
    generator.prepare()
    generator.impactOccurred()

    // Do Something
}

The above code is not working!

On Button click I want to use the taptic to make user user tap the button! Same thing I would like to use for picker view and other actions where user validate there inputs.

Let me understand is taptic is same small mechanical movementum?

2

There are 2 best solutions below

0
On

Make sure this is enabled in settings. If not,

Turn on System Haptic

Go to Settings. Tap on Sounds & Haptics. Scroll all the way down to System Haptics. Toggle this option on.

0
On

This is a list of all the possible types of Haptic Feedback you can use. All of these should work. I don't see anything necessarily wrong with your code might be a connection issue as well. Make sure the button is not disabled, its connected and or not behind something.

Also yes the 6s does support Haptic feedback.

@IBAction func errorButtonTapped(_ sender: UIButton) {
    let generator = UINotificationFeedbackGenerator()
    generator.notificationOccurred(.error)
}

@IBAction func successButtonTapped(_ sender: UIButton) {
    let generator = UINotificationFeedbackGenerator()
    generator.notificationOccurred(.success)
}

@IBAction func warningButtonTapped(_ sender: UIButton) {
    let generator = UINotificationFeedbackGenerator()
    generator.notificationOccurred(.warning)
}

@IBAction func lightButtonTapped(_ sender: UIButton) {
    let generator = UIImpactFeedbackGenerator(style: .light)
    generator.prepare()
    generator.impactOccurred()
}

@IBAction func mediumButtonTapped(_ sender: UIButton) {
    let generator = UIImpactFeedbackGenerator(style: .medium)
    generator.prepare()
    generator.impactOccurred()
}

@IBAction func heavyButtonTapped(_ sender: UIButton) {
    let generator = UIImpactFeedbackGenerator(style: .heavy)
    generator.prepare()
    generator.impactOccurred()
}