让iPhone振动

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)

  • 您还需要添加AudioToolbox.framework (11认同)
  • 我想补充一点,如果在iOS设置中关闭振动,即使使用这些命令,用户也不会受到振动. (11认同)
  • 有没有办法将振动时间减少到不到1秒? (10认同)
  • 在Swift中:`AudioServicesPlayAlertSound(UInt32(kSystemSoundID_Vibrate))`(至少从beta 2开始) (7认同)
  • #import <Cocoa/Cocoa.h>不是必需的. (3认同)
  • 谢谢.我要导入什么? (2认同)

Can*_*Can 45

Swift 2.0+

AudioToolbox现在将其显示kSystemSoundID_VibrateSystemSoundID类型,因此代码为:

import AudioToolbox.AudioServices

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
Run Code Online (Sandbox Code Playgroud)

而不是必须通过额外的演员步骤

(@Dov的道具)

原始答案(Swift 1.x)

而且,这是你如何在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.编译器给你一个错误,试图从中转换IntUInt32,但错误读作"无法转换为SystemSoundID",这是令人困惑的.为什么苹果不能让它成为Swift枚举超出我的范围.

@ aponomarenko's详细介绍,我的答案仅适用于那里的Swifters.


fsa*_*int 35

一种简单的方法是使用音频服务:

#import <AudioToolbox/AudioToolbox.h> 
...    
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)

  • #import <AudioToolbox/AudioToolbox.h>,您需要将AudioToolbox.framework添加到项目的Build Phases中. (3认同)

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)

  • 使用命名别名而不是魔术常量总是更好,比如kSystemSoundID_Vibrate而不是1352.我鼓励你更新你的答案. (7认同)
  • 我写了一个for循环,整数从已发布的常量开始,并找出哪一个导致振动 (4认同)
  • 我同意使用这个魔法1352并不理想,但即使振动开关在设备上关闭,我也找不到任何其他强制振动的方法.这似乎是唯一的方法. (3认同)
  • 即使在iPhone上激活静音模式,我也可以确认iPhone会振动.很棒的答案! (2认同)
  • 自2016年1月起,AudioServicesPlaySystemSound(1352)仍可用于iPhone,无论静音开关位置如何 (2认同)

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


Ben*_*enj 9

对于 iPhone 7/7 Plus 或更新机型,请使用这三个 Haptic 反馈 API。

可用的 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 时,有几件事值得记住。

注释 A

您实际上并没有创建触觉。请求系统生成触觉。系统将根据以下情况作出决定:

  • 如果设备上可以使用触觉(在这种情况下它是否具有 Taptic Engine)
  • 应用程序是否可以录制音频(录制过程中不会产生触觉以防止不必要的干扰)
  • 是否在系统设置中启用触觉。

因此,如果不可能,系统将默默地忽略您对触觉的请求。如果这是由于不受支持的设备造成的,您可以尝试以下操作:

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 设备。它将产生尽可能高的触觉。

备注 B

  • 由于生成触觉是一项硬件级任务,因此在调用触觉生成代码和实际发生之间可能存在延迟为此,Taptic Engine API 都有一个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)


Whe*_*lie 5

如果你正在使用Xamarin(monotouch)框架,只需打电话

SystemSound.Vibrate.PlayAlertSound()
Run Code Online (Sandbox Code Playgroud)


Seb*_*eek 5

在我的旅行中,我发现如果您在录制音频时尝试以下任一操作,即使启用了设备,设备也不会振动.

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实例.

希望这能帮助以前遇到过同样问题的其他人.


sam*_*ize 5

在 iOS 10 和较新的 iPhone 上,您还可以使用触觉 API。这种触觉反馈比 AudioToolbox API 更柔和。

对于您的 GAME OVER 场景,重 UI 影响反馈应该是合适的。

UIImpactFeedbackGenerator(style: .heavy).impactOccurred()

您可以使用其他触觉反馈样式