自定义iPhone振动的强度 [英] Intensity of custom iPhone vibration
问题描述
这是一个与相关的问题。是否有自定义振动的API iOS的?。
我能够创建自定义振动模式,但无法控制强度。
This is a question related to Are there APIs for custom vibrations in iOS?. I am able to create custom vibration patterns, but have no control over the intensity.
这是从Kevin Cao的答案中复制的,它可以启用自定义振动模式:
This is copied over from Kevin Cao's answer that enables custom vibration patterns:
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);
添加密钥的代码行 @Intensity
使用 int
值不起作用,我不知道如何查看 AudioServicesPlaySystemSoundWithVibration
弄明白的方法。我需要传递什么才能真正改变强度?
现在,如果我通过1,1000,0.4或0.0001并不重要,它总是相同的强度(在带有iOS7的iPhone 4上)。 任何人都可以重新创建吗?
The line of code that adds the key @"Intensity"
with an int
value doesn't do the trick and I don't know how to look inside the AudioServicesPlaySystemSoundWithVibration
method to figure it out. What do I have to pass to it so that it actually changes the intensity?
Right now, it doesn't matter if I pass 1, 1000, 0.4 or 0.0001, it's always the same intensity (on an iPhone 4 with iOS7). Can anyone recreate this?
我希望不仅可以创建振动模式,还可以创建平滑的振动包络。 如何?
I would like to be able not only to create vibration patterns, but a smooth vibration envelope. How to?
(因为这是一个用于仪器设计的研究项目,我(还)不关心应用程序商店的限制。)
推荐答案
更改 numberWithInt
调用 numberWithFloat
,并更改强度,使其介于0和1之间。我认为使用<$ c $时很奇怪c> int 而不是 float
。
Change the numberWithInt
call into numberWithFloat
, and change the intensity so it's between 0 and 1. I thought it was weird when they used an int
rather than a float
.
#pragma mark - Custom vibration methods
-(void)invokeCustomVibrationWithStartStopTimes:(NSArray*)startStopTimes andIntensity:(float)intensity {
BOOL startOrStop = YES;
NSMutableArray* arr = [@[] mutableCopy];
double time = 0;
for (NSNumber *x in stopStartTimes) {
[arr addObject:x]
startOrStop = !startOrStop;
[arr addObject:@(startOrStop)];
time = [x doubleValue] / 1000.0;
}
AudioServicesPlaySystemSoundWithVibration(4095,nil,{@"VibePattern":arr,@"Intensity":@(intensity)})
[self performSelector:@selector(stop) withObject:nil afterDelay:time];
}
-(void)stop {
AudioServicesStopSystemSound(4095); // stop buzzing the phone
}
startStopTimes
,它应该在开始时间和停止时间之间交替。传入这个数组:
For startStopTimes
, it should alternate between times started and times stopped. Passing in this array:
@[@(2000), @(1000), @(1000), @(500)]
将执行示例代码所做的事情。在这种情况下,它将启动2000毫秒,停止1000毫秒,启动1000毫秒,并停止500毫秒。
Will do what the example code did. In this case, it will start for 2000 ms, stop for 1000 ms, start for 1000 ms, and stop for 500 ms.
停止<调用/ code>来停止声音。我设置它的方式,它会在发送总时间后停止声音。
stop
is called to stop the sound. The way I have it set up, it stops sounds after the total amount of time sent in.
你可能已经注意到我一直在使用数组/数字文字而不是使用 [NSArray arrayWithObjects:...,nil];
或 [NSNumber numberWith ...];
。这使您的代码更短。另外,我用 #pragma mark
标记了开头。用它来更好地组织它。希望它有所帮助!
You may have noticed I've been using array/number literals rather than using [NSArray arrayWithObjects: ... , nil];
or [NSNumber numberWith...];
. This makes your code a lot shorter. Also, I marked the beginning with a #pragma mark
. Use that to organize it better. Hope it helps!
这篇关于自定义iPhone振动的强度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!