从iOS 5开始,用户可以为alert 和铃声创建自定义振动模式.以下屏幕截图显示了创建一个联系人应用程序的UI(iOS 6中的联系人应用程序):

iOS6中创建自定义振动的UI截图

我一直在四处搜索,包括文档,但我找不到任何公开创建或回放自定义振动的公共API.最接近的是使用AudioToolbox框架来播放短暂、恒定的振动:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Does anyone know if there are APIs for custom vibrations?它不一定是公共API.我很想知道Contacts应用程序使用了什么.有人知道吗?

P.S. Others have suggested 101 in 102 (100). I tried it, but couldn't get any vibration going for some reason.

October 2015 Update:

iOS 9中有新的专用API可与兼容设备中的Taptic引擎交互.有关更多信息,请参见Taptic in iOS 9.

推荐答案

经过几个小时的钻研,我终于弄明白了联系人应用的工作原理.

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.

Ios相关问答推荐

如何从Windows PC在iPhone上安装Flutter 应用程序

使用标头UICollectionViewFlowLayout时的简单SwiftUI视图错误

当应用程序被杀死时是否还能获取位置?

在 Android 和 iOS 应用程序上下文中阻止后台线程有哪些缺点?

iOS上从谷歌云存储API不断收到网络连接已丢失的错误信息

iOS应用启动时崩溃问题解决方法-NO_CRASH_STACK-DYLD 4个符号丢失

创建 Flutter 项目时,如何从我的系统中删除特定的开发者身份?

无法从 Xcode 获取模拟器列表.请打开 Xcode 并try 直接从那里运行项目以解决剩余问题

如何使用 AppIntents 在 SwiftUI 应用程序中打开特定视图

在 Swift 中映射 MySQL 数据

如何仅更改一个视图的导航标题 colored颜色 ?

从远程通知启动时,iOS 应用程序加载错误

警告:iPad:Icon-72.png:图标尺寸(0 x 0)

无法将NSTaggedPointerString类型的值转换为NSNumber

导航控制器自定义过渡动画

使用 swift 将本地 html 加载到 UIWebView

闭包不能隐式捕获变异的 self 参数

如何进行键值观察并在 UIView 的框架上获取 KVO 回调?

在 SKScene 中设置按钮

如何检测快速touch 或单击的tableView单元格