iOS震动反馈

长震动

AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil);
//未来即将弃用
//AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  //4095

支持iPhone基本机型,支持iOS9
注:用户在“系统设置”中关闭了振动功能,此震动会失效。

短震动一

// 普通短震,3D Touch 中 Peek 震动反馈
AudioServicesPlaySystemSoundWithCompletion(1519, nil);
// 普通短震,3D Touch 中 Pop 震动反馈
AudioServicesPlaySystemSoundWithCompletion(1520, nil);
// 连续三次短震
AudioServicesPlaySystemSoundWithCompletion(1521, nil);
// 'Try Again' feedback (week boom then strong boom)
AudioServicesPlaySystemSoundWithCompletion(1102, nil);

支持iPhone 6s及其以上机型
注:可能会涉及私有属性,影响iOS审核,请谨慎选择。
关于是否调用了私有 API,也有一些讨论,可以查看这里。

短震动二

#pragma mark -
#pragma mark - UINotificationFeedbackGenerator

+ (void)executeSuccessFeedback
{
    if (@available(iOS 10.0, *)) {
        UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
        [generator notificationOccurred:UINotificationFeedbackTypeSuccess];
    }
}

+ (void)executeWarningFeedback
{
    if (@available(iOS 10.0, *)) {
        UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
        [generator notificationOccurred:UINotificationFeedbackTypeWarning];
    }
}

+ (void)excuteErrorFeedback
{
    if (@available(iOS 10.0, *)) {
        UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
        [generator notificationOccurred:UINotificationFeedbackTypeError];
    }
}

#pragma mark -
#pragma mark - UIImpactFeedbackGenerator

+ (void)excuteLightFeedback
{
    if (@available(iOS 10.0, *)) {
        UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight];
        [generator prepare];
        [generator impactOccurred];
    }
}

+ (void)excuteMediumFeedback
{
    if (@available(iOS 10.0, *)) {
        UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleMedium];
        [generator prepare];
        [generator impactOccurred];
    }
}

+ (void)excuteHeavyFeedback
{
    if (@available(iOS 10.0, *)) {
        UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleHeavy];
        [generator prepare];
        [generator impactOccurred];
    }
}

支持iPhone 7及其以上机型,iOS10及其以上版本

短震动三

+ (void)excuteSoftFeedback
{
    if (@available(iOS 13.0, *)) {
        UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleSoft];
        [generator prepare];
        [generator impactOccurred];
    }
}

+ (void)excuteRigidFeedback
{
    if (@available(iOS 13.0, *)) {
        UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleRigid];
        [generator prepare];
        [generator impactOccurred];
    }
}

支持iOS13及其以上版本

关于自定义震动

如果你想自定义震动,或者想在iPhone基本机型,iOS9系统下实现短震,可以尝试使用私有API来实现。
但是同样的也要承受App审核被拒的风险。

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);
AudioServicesStopSystemSound();

具体讨论可参考这里

Demo

以上的震动反馈写了一个Demo,可以下载运行到真机上自行体验。
github

你可能感兴趣的:(iOS震动反馈)