震动效果调研 - iOS

震动效果调研 - iOS,第1张

目录

一、震动需要的权限

二、震动参数

总结:


本文档为作者学习文档,如果转载请联系作者

一、震动需要的权限

在手机设置中,有一个声音与触感的选项,选项中有两个震动权限

二、震动参数

第一种方法:全版本通用的震动,通过苹果私有API实现

在需要的类中导入
#import 
在所需要的地方添加参数
// 1.长震动方法
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

// 2.普通短振(3D Touch 中 Peek 震动反馈)
AudioServicesPlaySystemSound(1519);

// 3.普通短振(3D Touch 中 Pop 震动反馈)
AudioServicesPlaySystemSound(1520);

// 4.连续三次震动
AudioServicesPlaySystemSound(1521);

注: 在使用长震动的时候,需要同时打开两个震动权限,只打开其中的一个无效的。其他三种震动不管打开还是关闭,震动均有效。

第二种方法:在iOS10以后,苹果新增了一套震动反馈API,但仅支持iPhone7之后有虚拟home键的设备

有五种参数的形式:

UIImpactFeedbackStyleLightUIImpactFeedbackStyleMediumUIImpactFeedbackStyleHeavyUIImpactFeedbackStyleSoft API_AVAILABLE(ios(13.0))UIImpactFeedbackStyleRigid API_AVAILABLE(ios(13.0))
if (@available(iOS 10.0, *)) {
       UIImpactFeedbackGenerator *r = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
       [r prepare];
       [r impactOccurred];
    } else {
        // Fallback on earlier versions
    }

注: 这五种参数形式的震动方法,不管开关是否打开,震动均有效。

第三种方法:通过UINotificationFeedbackGenerator来实现

有三种参数形式:

UINotificationFeedbackTypeSuccessUINotificationFeedbackTypeWarningUINotificationFeedbackTypeError
if (@available(iOS 10.0, *)) {
        UINotificationFeedbackGenerator *r = [[UINotificationFeedbackGenerator alloc] init];
        [r notificationOccurred:UINotificationFeedbackTypeWarning];
    } else {
        // Fallback on earlier versions
    }

注: 这三种参数形式的震动方法,不管开关是否打开,震动均有效。

第四种震动方法:通过使用Core Haptics框架来实现

Core Haptics简介:Core Haptics使您可以向应用程序添加自定义的触觉和音频反馈。使用触觉,通过触觉和音频反馈吸引用户并增强动作,从而与用户进行身体互动。该框架包含音频和振动反馈,可以控制振动的强度,频率,时间等。 无论您选择生成自定义触觉的哪种构建块,都可以控制其强度和尖锐度(intensity and sharpness)。强度会改变触觉的幅度或力度。尖锐度使您可以确定触觉体验的特征。

CHHapticEngineCHHapticPatternCHHapticEvent

主要有以下四种枚举:

typedef NSString *CHHapticEventType NS_TYPED_ENUM;

CH_EXPORT
CHHapticEventType CHHapticEventTypeHapticTransient API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT
CHHapticEventType CHHapticEventTypeHapticContinuous API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT
CHHapticEventType CHHapticEventTypeAudioContinuous API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT
CHHapticEventType CHHapticEventTypeAudioCustom API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);

分别表示震动短暂的,可连续的;音频可连续的,通常的。

==注:==可连续一定要使用包含duration这个方法。

// 任意长度
event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:@[parameter1, parameter2] relativeTime:0 duration:2];
CHHapticEventParameter CHHapticEventParameterID 用于修改单个触觉和/或音频事件的参数。
typedef NSString *CHHapticEventParameterID NS_TYPED_ENUM;

CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDHapticIntensity API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDHapticSharpness API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);

CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDAttackTime API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDDecayTime API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDReleaseTime API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDSustained API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);

CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDAudioVolume API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDAudioPitch API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDAudioPan API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticEventParameterID CHHapticEventParameterIDAudioBrightness API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CHHapticDynamicParameterID 用于动态修改模式内所有触觉或音频事件的参数。
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDHapticIntensityControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDHapticSharpnessControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDHapticAttackTimeControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDHapticDecayTimeControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDHapticReleaseTimeControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);

CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDAudioVolumeControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDAudioPanControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDAudioBrightnessControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDAudioPitchControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDAudioAttackTimeControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDAudioDecayTimeControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);
CH_EXPORT CHHapticDynamicParameterID CHHapticDynamicParameterIDAudioReleaseTimeControl API_AVAILABLE(ios(13.0), macos(10.15), tvos(14.0), macCatalyst(13.0)) API_UNAVAILABLE(watchos);

最终使用:

/// 添加振动
- (void)addFeedback {
    NSError *error = nil;
    self.engine = [[CHHapticEngine alloc] initAndReturnError:&error];

    DataModel *model = [DataModel shareInstance];
    /// 强度
    CHHapticEventParameter *parameter1 = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:model.strengthIndex];
    /// 频率
    CHHapticEventParameter *parameter2 = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticSharpness value:model.frequency];

    CHHapticEvent *event;
    if ([model.type isEqualToString:@"1"]) {
         // 任意长度
        event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:@[parameter1, parameter2] relativeTime:0 duration:[model.time floatValue]];
    }else {
        // 短暂
        event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticTransient parameters:@[parameter1, parameter2] relativeTime:0];
    }
    CHHapticPattern *patten = [[CHHapticPattern alloc] initWithEvents:@[event] parameterCurves:@[] error:&error];

    id player = [self.engine createPlayerWithPattern:patten error:&error];

    [self.engine startAndReturnError:&error];

    [player startAtTime:0 error:&error];
    
}

注: 这里CHHapticEngine一定要是全局变量,如果是局部,会导致用完释放,下次点击没有声音。

还可以将样式编写在AHAP文件中,通过读取URL进行震动效果。

例如:

{
    "Pattern":
    [
        {
            "Event":
            {
                "Time": 0.0,
                "EventType": "HapticTransient",
                "EventParameters":
                [
                    { "ParameterID": "HapticIntensity", "ParameterValue": 0.8 },
                    { "ParameterID": "HapticSharpness", "ParameterValue": 0.4 }
                ]
            }
        },
        {
            "Event":
            {
                "Time": 0.015,
                "EventType": "HapticContinuous",
                "EventDuration": 0.25,
                "EventParameters":
                [
                    { "ParameterID": "HapticIntensity", "ParameterValue": 0.8 },
                    { "ParameterID": "HapticSharpness", "ParameterValue": 0.4 }
                ]
            }
        },
        {
            "ParameterCurve":
            {
                "ParameterID": "HapticIntensityControl",
                "Time": 0.015,
                "ParameterCurveControlPoints":
                [
                    { "Time": 0, "ParameterValue": 1 },
                    { "Time": 0.1, "ParameterValue": 0.5 },
                    { "Time": 0.25, "ParameterValue": 0.0 }
                ]
            }
        },
        {
            "ParameterCurve":
            {
                "ParameterID": "HapticSharpnessControl",
                "Time": 0.015,
                "ParameterCurveControlPoints":
                [
                    { "Time": 0, "ParameterValue": 0.0 },
                    { "Time": 0.25, "ParameterValue": -0.3 }
                ]
            }
        }
    ]
}

调用AHAP实现发现震动代码:

- (void)readFileShake {
    NSError *error = nil;
    if (@available(iOS 13.0, *)) {
        self.engineTest = [[CHHapticEngine alloc] initAndReturnError:&error];
        NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"Drums" ofType:@"ahap"];
        NSURL *url = [[NSURL alloc]initFileURLWithPath:musicFilePath];
       
        if ([self.engineTest playPatternFromURL:url error:&error]) {
            [self.engineTest startAndReturnError:&error];
        }
    } else {
        // Fallback on earlier versions
    }
   
}
总结:

综合以上三种方法:目前推荐方案3,针对ios10以上的版本生效此功能

Demo地址

参考文档:苹果官方文档​​​​​​

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/989599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存