jar*_*ryd 226 cocoa-touch avfoundation vibration ios
如何将iPhone设置为振动一次?
例如,当玩家失去生命或游戏结束时,iPhone应该振动.
lin*_*ild 402
从" iPhone教程:检查iOS设备功能的更好方法 ":
有两个看似相似的函数接受一个参数kSystemSoundID_Vibrate:
1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)
这两个功能都会震动iPhone.但是,当您在不支持振动的设备上使用第一个功能时,它会发出哔声.另一方面,第二个功能在不支持的设备上不执行任何操作.因此,如果您要持续振动设备,作为警报,常识说,请使用功能2.
首先,AudioToolbox.framework在Build Phases 中将AudioToolbox框架添加到目标.
然后,导入此头文件:
#import <AudioToolbox/AudioServices.h>
Run Code Online (Sandbox Code Playgroud)
Can*_*Can 45
AudioToolbox现在将其显示kSystemSoundID_Vibrate为SystemSoundID类型,因此代码为:
import AudioToolbox.AudioServices
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
Run Code Online (Sandbox Code Playgroud)
而不是必须通过额外的演员步骤
(@Dov的道具)
而且,这是你如何在Swift上做的(如果你遇到了和我一样的麻烦)
链接AudioToolbox.framework(转到您的项目,选择目标,构建阶段,链接二进制文件库,在那里添加库)
完成后:
import AudioToolbox.AudioServices
// Use either of these
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
Run Code Online (Sandbox Code Playgroud)
俗气的是,这SystemSoundID基本上是一个typealias(花哨的快速typedef)a UInt32,而且kSystemSoundID_Vibrate是一个常规的Int.编译器给你一个错误,试图从中转换Int为UInt32,但错误读作"无法转换为SystemSoundID",这是令人困惑的.为什么苹果不能让它成为Swift枚举超出我的范围.
@ aponomarenko's详细介绍,我的答案仅适用于那里的Swifters.
fsa*_*int 35
一种简单的方法是使用音频服务:
#import <AudioToolbox/AudioToolbox.h>
...
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)
Joe*_*ply 29
对于以某种方式关闭振动的设备,我遇到了很大的麻烦,但我们需要它无论如何工作,因为它对我们的应用程序运行至关重要,并且因为它只是一个记录的方法调用的整数,它将通过验证.所以我尝试了一些声音,这些声音不在这里有详细记录的声音:TUNER88/iOSSystemSoundsLibrary
然后我偶然发现1352,无论静音开关或设备上的设置如何,它都能正常工作(Settings->vibrate on ring, vibrate on silent).
- (void)vibratePhone;
{
if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
{
AudioServicesPlaySystemSound (1352); //works ALWAYS as of this post
}
else
{
// Not an iPhone, so doesn't have vibrate
// play the less annoying tick noise or one of your own
AudioServicesPlayAlertSound (1105);
}
}
Run Code Online (Sandbox Code Playgroud)
Hug*_*nso 25
重要提示:未来弃权的警示.
从iOS 9.0开始,API函数描述为:
AudioServicesPlaySystemSound(inSystemSoundID: SystemSoundID)
AudioServicesPlayAlertSound(inSystemSoundID: SystemSoundID)
Run Code Online (Sandbox Code Playgroud)
包括以下注释:
This function will be deprecated in a future release.
Use AudioServicesPlayAlertSoundWithCompletion or
AudioServicesPlaySystemSoundWithCompletion instead.
Run Code Online (Sandbox Code Playgroud)
正确的方法是使用这两个中的任何一个:
AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate, nil)
Run Code Online (Sandbox Code Playgroud)
要么
AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate) {
//your callback code when the vibration is done (it may not vibrate in iPod, but this callback will be always called)
}
Run Code Online (Sandbox Code Playgroud)
记得
import AVFoundation
对于 iPhone 7/7 Plus 或更新机型,请使用这三个 Haptic 反馈 API。
let generator = UINotificationFeedbackGenerator()
generator.notificationOccured(style: .error)
Run Code Online (Sandbox Code Playgroud)
可用的样式.error,.success和.warning。每个人都有自己独特的感觉。
从文档:
UIFeedbackGenerator创建触觉以传达成功、失败和警告的具体子类。
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccured()
Run Code Online (Sandbox Code Playgroud)
可用的样式.heavy,.medium和.light。这些是具有不同程度“硬度”的简单振动。
从文档:
UIFeedbackGenerator创建触觉以模拟物理影响的具体子类
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
Run Code Online (Sandbox Code Playgroud)
这是所有触觉中最不引人注目的,因此最适合触觉不应该接管应用程序体验的情况。
从文档:
UIFeedbackGenerator创建触觉以指示选择更改的具体子类。
在使用这些 API 时,有几件事值得记住。
您实际上并没有创建触觉。您请求系统生成触觉。系统将根据以下情况作出决定:
因此,如果不可能,系统将默默地忽略您对触觉的请求。如果这是由于不受支持的设备造成的,您可以尝试以下操作:
func haptic() {
// Get whether the device can generate haptics or not
// If feedbackSupportLevel is nil, will assign 0
let feedbackSupportLevel = UIDevice.current.value(forKey: "_feedbackSupportLevel") as? Int ?? 0
switch feedbackSupportLevel {
case 2:
// 2 means the device has a Taptic Engine
// Put Taptic Engine code here, using the APIs explained above
case 1:
// 1 means no Taptic Engine, but will support AudioToolbox
// AudioToolbox code from the myriad of other answers!
default: // 0
// No haptic support
// Do something else, like a beeping noise or LED flash instead of haptics
}
Run Code Online (Sandbox Code Playgroud)
替换switch-case语句中的注释,此触觉生成代码将可移植到其他 iOS 设备。它将产生尽可能高的触觉。
prepare()方法,可以将其置于就绪状态。使用您的游戏结束示例:您可能知道游戏即将结束,因为用户 HP 非常低,或者附近有危险的怪物。
在这种情况下,准备 Taptic Engine 将创造更高质量、更灵敏的体验。
例如,假设您的应用程序使用平移手势识别器来更改可见世界的部分。您希望在用户 360 度“环视”时生成触觉。这是您可以使用的方法prepare():
@IBAction func userChangedViewablePortionOfWorld(_ gesture: UIPanGestureRecogniser!) {
haptic = UIImpactFeedbackGenerator(style: .heavy)
switch gesture.state {
case .began:
// The user started dragging the screen.
haptic.prepare()
case .changed:
// The user trying to 'look' in another direction
// Code to change viewable portion of the virtual world
if virtualWorldViewpointDegreeMiddle = 360.0 {
haptic.impactOccured()
}
default:
break
}
Run Code Online (Sandbox Code Playgroud)
如果你正在使用Xamarin(monotouch)框架,只需打电话
SystemSound.Vibrate.PlayAlertSound()
Run Code Online (Sandbox Code Playgroud)
在我的旅行中,我发现如果您在录制音频时尝试以下任一操作,即使启用了设备,设备也不会振动.
1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)
在测量设备运动的特定时间调用我的方法.我不得不停止录音,然后在振动发生后重新开始录音.
看起来像这样.
-(void)vibrate {
[recorder stop];
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
[recorder start];
}
Run Code Online (Sandbox Code Playgroud)
recorder 是一个AVRecorder实例.
希望这能帮助以前遇到过同样问题的其他人.