如何将iPhone设置为震动一次?

例如,当玩家失去一条生命或游戏结束时,iPhone应该震动。


当前回答

你可以使用

1) AudioServicesPlayAlertSound (kSystemSoundID_Vibrate);

iPhone和一些新款ipod。

2) AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);

ipad。

其他回答

在我的旅行中,我发现如果你在录制音频时尝试以下任何一种方法,即使启用了它,设备也不会振动。

1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

我的方法在测量设备运动的特定时间被调用。我不得不停止录音,然后在震动发生后重新开始录音。

它是这样的。

-(void)vibrate {
    [recorder stop];
    AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    [recorder start];
}

recorder是一个AVRecorder实例。

希望这能帮助到以前有同样问题的人。

对于iPhone 7/7 Plus或更新版本,使用这三个触觉反馈api。

可用的api

通知:

let generator = UINotificationFeedbackGenerator()
generator.notificationOccured(style: .error)

可用的样式有。error、。success和。warning。每一种都有自己独特的感觉。 从文档中可以看出:

一个具体的UIFeedbackGenerator子类,创建触觉来传达成功、失败和警告。

对于简单的振动:

let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccured()

可用的样式有。重型、。中型和。轻型。这些都是具有不同“硬度”的简单振动。 从文档中可以看出:

一个具体的UIFeedbackGenerator子类,创建触觉来模拟物理冲击

当用户选择一个项目时使用

let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()

这是所有触感中最不引人注意的,所以最适合当触感不应该接管应用体验的时候。 从文档中可以看出:

一个具体的UIFeedbackGenerator子类,它创建触觉来指示选择的变化。

笔记

在使用这些api时,有几件事值得记住。

注意一个

你并没有真正创造触觉。你请求系统生成一个触觉图。系统将根据以下决定:

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

因此,如果不可能,系统将无声地忽略您的触觉请求。如果这是由于不支持的设备,您可以尝试这样做:

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
    }

替换掉switch-case语句中的注释,这个触觉生成代码就可以移植到其他iOS设备上。它将产生最高水平的触觉。

注意B

Due to the fact that generating haptics is a hardware-level task, there may be latency between when you call the haptic-generation code, and when it actually happens. For this reason, the Taptic Engine APIs all have a prepare() method, to put it in a state of readiness. Using your Game Over example: You may know that the game is about to end, by the user having very low HP, or a dangerous monster being near them. If you don't generate a haptic within a few seconds, the Taptic Engine will go back into an idle state (to save battery life)

在这种情况下,准备Taptic引擎将创建更高质量、更灵敏的体验。

例如,假设你的应用程序使用平移手势识别器来改变世界可见的部分。当用户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

} 

在我的例子中,我使用的是AVCaptureSession。 AudioToolbox是在项目的构建阶段,它被导入,但仍然不能工作。为了使它工作,我在振动之前停止会话,然后继续。

#import <AudioToolbox/AudioToolbox.h>
...
@property (nonatomic) AVCaptureSession *session;
...
- (void)vibratePhone;
{
  [self.session stopRunning];
     NSLog(@"vibratePhone %@",@"here");
    if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
    {
        AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); 
    }
    else
    {
        AudioServicesPlayAlertSound (kSystemSoundID_Vibrate);
    }
  [self.session startRunning];
}

重要提示:未来弃用警报。 从iOS 9.0开始,API函数描述如下:

AudioServicesPlaySystemSound(inSystemSoundID: SystemSoundID)
AudioServicesPlayAlertSound(inSystemSoundID: SystemSoundID)

包括以下说明:

This function will be deprecated in a future release.
Use AudioServicesPlayAlertSoundWithCompletion or  
AudioServicesPlaySystemSoundWithCompletion instead.

正确的方法是使用以下两种:

AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate, nil)

or

AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate) {
 //your callback code when the vibration is done (it may not vibrate in iPod, but this callback will be always called)
}

记得导入AVFoundation

在iOS 10和更新的iphone上,你也可以使用触觉API。这种触觉反馈比AudioToolbox API更柔和。

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

UIImpactFeedbackGenerator(风格:.heavy) .impactOccurred ()

你可以使用其他的触觉反馈方式。