在联系人应用程序中挖掘了几个小时后,我已经弄清楚了它是如何工作的。
ABNewPersonViewController调用ToneLibrary框架中的一些类来完成此操作。
调用堆栈看起来像这样:
0 CoreFoundation 0x3359a1d4 CFGetTypeID + 0
1 CoreFoundation 0x33596396 __CFPropertyListIsValidAux + 46
2 CoreFoundation 0x33517090 CFPropertyListCreateData + 124
3 AudioToolbox 0x38ac255a AudioServicesPlaySystemSoundWithVibration + 158
5 ToneLibrary 0x35a7811a -[TLVibrationRecorderView vibrationComponentDidStartForVibrationRecorderTouchSurface:] + 38
6 ToneLibrary 0x35a772b2 -[TLVibrationRecorderTouchSurface touchesBegan:withEvent:] + 342
7 UIKit 0x3610f526 -[UIWindow _sendTouchesForEvent:] + 314
8 UIKit 0x360fc804 -[UIApplication sendEvent:] + 376
在网上搜索“AudioServicesPlaySystemSoundWithVibration”后,我没有找到任何信息。
所以我决定自己研究一下。它是AudioToolbox框架中的私有函数。
该函数的声明如下:
void AudioServicesPlaySystemSoundWithVibration(SystemSoundID inSystemSoundID,id arg,NSDictionary* vibratePattern)
"inSystemSoundID"是SystemSoundID,就像"AudioServicesPlaySystemSound"一样,传入"kSystemSoundID_Vibrate"。
"arg"不重要,将其传入nil,一切仍将正常工作。
"vibratePattern"是一个指向"NSDictionary"的指针,联系人应用程序传入{Intensity = 1; OffDuration = 1; OnDuration = 10;}来记录用户输入。
但只调用此函数将使振动永远不停止。所以我必须找到一些函数来停止它。
答案是"AudioServicesStopSystemSound"。它也是AudioToolbox框架中的私有函数。
函数的声明如下:
void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)
我猜联系人应用程序在touchesBegan方法中使用AudioServicesPlaySystemSoundWithVibration,而在touchEnd方法中使用AudioServicesStopSystemSound来实现此效果。
TLVibrationController将管理器一个振动模式对象以记录您输入的过程。
最后,它会生成一个字典传递给AudioServicesPlaySystemSoundWithVibration,以像下面这样重新播放整个过程:
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* arr = [NSMutableArray array ];
[arr addObject:[NSNumber numberWithBool:YES]];
[arr addObject:[NSNumber numberWithInt:2000]];
[arr addObject:[NSNumber numberWithBool:NO]];
[arr addObject:[NSNumber numberWithInt:1000]];
[arr addObject:[NSNumber numberWithBool:YES]];
[arr addObject:[NSNumber numberWithInt:1000]];
[arr addObject:[NSNumber numberWithBool:NO]];
[arr addObject:[NSNumber numberWithInt:500]];
[dict setObject:arr forKey:@"VibePattern"];
[dict setObject:[NSNumber numberWithInt:1] forKey:@"Intensity"];
AudioServicesPlaySystemSoundWithVibration(4095,nil,dict);
如果您想在iOS上自定义振动,请使用AudioServicesPlaySystemSoundWithVibration和AudioServicesStopSystemSound。