This commit is contained in:
2026-07-15 15:30:48 +08:00
parent b1a7167ca7
commit 62801f22e9
5 changed files with 172 additions and 1 deletions
@@ -0,0 +1,8 @@
// HapticFeedbackManager.h
#import <Foundation/Foundation.h>
@interface HapticFeedbackManager : NSObject
+ (void)TriggerCustomHaptic:(float)intensity sharpness:(float)sharpness duration:(float)duration;
@end
@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: c9ce6d3a070acdf4394bf7556c953c27
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:
@@ -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
@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 4c2e3a62020ffae4998ebaa981d7c1e6
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:
+3 -1
View File
@@ -37,6 +37,8 @@ void SetBtn(int left, int top, int right, int bottom){
void OpenWv (bool show) {
[[H5View shared] OpenWv];
}
void showGameA(bool flag){//显示游戏a
[NativeUIManager showMainMenu];
}
}