提交
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
// HapticFeedbackManager.m
|
||||
#import "HapticFeedbackManager.h"
|
||||
#import <CoreHaptics/CoreHaptics.h>
|
||||
#import <AudioToolbox/AudioToolbox.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@implementation HapticFeedbackManager
|
||||
|
||||
static CHHapticEngine *hapticEngine;
|
||||
|
||||
+ (void)initializeHapticEngine {
|
||||
if (!hapticEngine) {
|
||||
NSError *error = nil;
|
||||
hapticEngine = [[CHHapticEngine alloc] initAndReturnError:&error];
|
||||
if (error) {
|
||||
NSLog(@"Failed to create haptic engine: %@", error.localizedDescription);
|
||||
} else {
|
||||
[hapticEngine startWithCompletionHandler:^(NSError * _Nullable error) {
|
||||
if (error) {
|
||||
NSLog(@"Failed to start haptic engine: %@", error.localizedDescription);
|
||||
}
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+ (void)TriggerCustomHaptic:(float)intensity sharpness:(float)sharpness duration:(float)duration {
|
||||
if (@available(iOS 13.0, *)) {
|
||||
|
||||
if (![CHHapticEngine capabilitiesForHardware].supportsHaptics) {
|
||||
NSLog(@"Haptic feedback is not supported on this device.");
|
||||
[self triggerFallbackHaptic];
|
||||
return;
|
||||
}
|
||||
|
||||
[self initializeHapticEngine];
|
||||
|
||||
NSError *error = nil;
|
||||
CHHapticEventParameter *intensityParameter = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:intensity];
|
||||
CHHapticEventParameter *sharpnessParameter = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticSharpness value:sharpness];
|
||||
CHHapticEvent *event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:@[intensityParameter, sharpnessParameter] relativeTime:0 duration:duration];
|
||||
|
||||
CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:@[event] parameters:@[] error:&error];
|
||||
if (error) {
|
||||
NSLog(@"Failed to create haptic pattern: %@", error.localizedDescription);
|
||||
return;
|
||||
}
|
||||
|
||||
id<CHHapticPatternPlayer> player = [hapticEngine createPlayerWithPattern:pattern error:&error];
|
||||
if (error) {
|
||||
NSLog(@"Failed to create haptic player: %@", error.localizedDescription);
|
||||
return;
|
||||
}
|
||||
|
||||
[player startAtTime:CHHapticTimeImmediate error:&error];
|
||||
if (error) {
|
||||
NSLog(@"Failed to start haptic player: %@", error.localizedDescription);
|
||||
}
|
||||
} else {
|
||||
NSLog(@"Core Haptics is not available on this version of iOS.");
|
||||
}
|
||||
}
|
||||
|
||||
+ (void)triggerFallbackHaptic {
|
||||
// 使用轻微震动作为替代
|
||||
// UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc]
|
||||
// initWithStyle:UIImpactFeedbackStyleLight];
|
||||
// [generator prepare];
|
||||
// [generator impactOccurred];
|
||||
|
||||
// UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc]
|
||||
// initWithStyle:UIImpactFeedbackStyleHeavy];
|
||||
// [generator prepare];
|
||||
// [generator impactOccurred];
|
||||
|
||||
// 使用 UINotificationFeedbackGenerator 触发通知风格的震动
|
||||
// UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
|
||||
// [generator prepare];
|
||||
// [generator notificationOccurred:UINotificationFeedbackTypeSuccess];
|
||||
|
||||
// 使用系统震动
|
||||
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
void TriggerCustomHaptic(float intensity, float sharpness, float duration) { [HapticFeedbackManager TriggerCustomHaptic:intensity sharpness:sharpness duration:duration];
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user