7

I search to make vibrate twice my iphone when I click on a button (like a sms alert vibration)

With AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) I obtain just one normal vibration but I want two shorts :/.

Cœur
  • 37,241
  • 25
  • 195
  • 267
sunstan
  • 113
  • 1
  • 1
  • 3
  • 1
    http://stackoverflow.com/questions/12966467/are-there-apis-for-custom-vibrations-in-ios – shim Apr 24 '15 at 05:18

4 Answers4

33

Update for iOS 10

With iOS 10, there are a few new ways to do this with minimal code.

Method 1 - UIImpactFeedbackGenerator:

let feedbackGenerator = UIImpactFeedbackGenerator(style: .heavy)
feedbackGenerator.impactOccurred()

Method 2 - UINotificationFeedbackGenerator:

let feedbackGenerator = UINotificationFeedbackGenerator()
feedbackGenerator.notificationOccurred(.error)

Method 3 - UISelectionFeedbackGenerator:

let feedbackGenerator = UISelectionFeedbackGenerator()
feedbackGenerator.selectionChanged()
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 1
    The UINotificationGenerator works only on devices that have a Taptic Engine. That's iPhone 7 and newer. I tried this code on my iPhone 6s, didn't work. I read a bit more about it and found out that it only works on devices that have a Taptic Engine and the System Haptics setting _must_ be enabled in System Settings, otherwise, it won't work. – Lazar Nikolov Jul 27 '18 at 08:52
6
#import <AudioToolbox/AudioServices.h>


AudioServicesPlayAlertSound(UInt32(kSystemSoundID_Vibrate))

This is the swift function...See this article for detailed description.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
vijeesh
  • 1,317
  • 1
  • 17
  • 32
  • This isn't working for me, maybe Apple changed something or perhaps I'm doing something wrong? It's only giving me a single vibrate. – jammyman34 Jun 07 '17 at 21:44
4

This is what I came up with:

import UIKit
import AudioToolbox

class ViewController: UIViewController {

    var counter = 0
    var timer : NSTimer?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func vibratePhone() {
        counter++
        switch counter {
        case 1, 2:
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        default:
            timer?.invalidate()
        }
    }

    @IBAction func vibrate(sender: UIButton) {
        counter = 0
        timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: "vibratePhone", userInfo: nil, repeats: true)
    }
}

When you press the button, the timer starts and repeats at desired time interval. The NSTimer calls the vibratePhone(Void) function and from there I can control how many times the phone will vibrate. I used a switch in this case, but you could use a if else, too. Simply set a counter to count each time the function is called.

Troy
  • 91
  • 2
2

If you want to vibrate only two times. You can just..

    func vibrate() {
        AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate) {
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        }
    }

And vibrating multiple times can be possible by using recursion and AudioServicesPlaySystemSoundWithCompletion.

You can pass a count number to vibrate function like vibrate(count: 10). Then it vibrates 10 times.

    func vibrate(count: Int) {
        if count == 0 {
            return
        }
        AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate) { [weak self] in
            self?.vibrate(count: count - 1)
        }
    }


In case of using UIFeedbackGenerator, There is a great library Haptica


Hope it helps.

Won
  • 1,107
  • 9
  • 16