经过几个小时的钻研,我终于弄明白了联系人应用的工作原理.
ABNewPersonViewControlle调用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
在网上搜索"AudioServicesPlaySystemSoundWithViviations"后,我什么也没有找到.
所以我决定自己go 查一查.它是AudioToolbox框架中的私有函数.
函数的声明类似于
void AudioServicesPlaySystemSoundWithVibration(SystemSoundID inSystemSoundID,id arg,NSDictionary* vibratePattern)
inSystemSoundID是SystemSoundID,和AudioServicesPlaySystemSound一样,传递kSystemSoundID_Viviate.
"arg"并不重要,把它归零,一切都会很好的.
"VibractPattern"是"NSDictionary"的指针,联系人应用程序进入
但只有调用此函数才能使振动永不停止.所以我必须找到一些功能来阻止它.
答案是"AudioServicesStopSystemSound".它也是AudioToolbox框架中的私有函数.
函数的声明类似于
void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)
我猜联系人App在TouchesBegan方法中使用AudioServicesPlaySystemSoundWithVivation,在TouchEnd方法中使用AudioServicesStopSystemSound来达到这个效果.
TLVibrationController将管理一个振动模式对象来记录您输入的过程.
最后,生成一个字典传入AudioServicesPlaySystemSoundWithViviation,回放整个过程,如下所示:
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* arr = [NSMutableArray array ];
[arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms
[arr addObject:[NSNumber numberWithInt:2000]];
[arr addObject:[NSNumber numberWithBool:NO]]; //stop for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];
[arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];
[arr addObject:[NSNumber numberWithBool:NO]]; //stop for 500ms
[arr addObject:[NSNumber numberWithInt:500]];
[dict setObject:arr forKey:@"VibePattern"];
[dict setObject:[NSNumber numberWithInt:1] forKey:@"Intensity"];
AudioServicesPlaySystemSoundWithVibration(4095,nil,dict);
因此,如果您想在iOS中自定义振动.使用AudioServicesPlaySystemSoundWith Vivation和AudioServicesStopSystemSound.