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 :/.
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 :/.
This is the swift function...See this article for detailed description.
This is what I came up with:
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.
If you want to vibrate only two times. You can just..
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.In case of using
UIFeedbackGenerator
, There is a great library HapticaHope it helps.
Update for iOS 10
With iOS 10, there are a few new ways to do this with minimal code.
Method 1 - UIImpactFeedbackGenerator:
Method 2 - UINotificationFeedbackGenerator:
Method 3 - UISelectionFeedbackGenerator: