For create custom vibrations in iOS. Use AudioServicesPlaySystemSoundWithVibration and AudioServicesStopSystemSound.
Heart Beat Vibration Example
NSMutableDictionary* pulsePatternsDict = [@{} mutableCopy];
NSMutableArray* pulsePatternsArray = [@[] mutableCopy];
// beat for 100 times
for (NSInteger i=0; i<100; i++){
[pulsePatternsArray addObject:@(YES)]; // vibrate for 100ms
[pulsePatternsArray addObject:@(100)];
[pulsePatternsArray addObject:@(NO)]; //stop for 1200ms * 0.3
[pulsePatternsArray addObject:@(1200*0.3)];
[pulsePatternsArray addObject:@(YES)]; //vibrate for 100ms
[pulsePatternsArray addObject:@(100)];
[pulsePatternsArray addObject:@(NO)]; //stop for 1200ms * 0.5
[pulsePatternsArray addObject:@(1200*0.5)];
}
[pulsePatternsDict setObject:pulsePatternsArray forKey:@"VibePattern"];
[pulsePatternsDict setObject:[NSNumber numberWithInt:1.0] forKey:@"Intensity"];
AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, pulsePatternsDict);
But only call this (AudioServicesPlaySystemSoundWithVibration ) will make a vibration never stop. So I have to found some function to stop it.
The answer is AudioServicesStopSystemSound
. It's also a private function in AudioToolbox framework.
The declaration of the function is like
void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)
NOTE: AudioServicesPlaySystemSoundWithVibration
is only available on iOS6
There is currently no public available API in iOS 9 and iOS 9.1.
And In iOS 10 there's a new API called UIFeedbackGenerator
. See this post for more details. It only works on the iPhone 7 and iPhone 7 Plus for now.
Disclaimer: There is a way to interact with Taptic Engine (iOS 9) directly, but there is a private method. You should not use it in App Store applications.