Vibration support on legacy devices

This commit is contained in:
Lior Halphon 2023-01-14 21:15:25 +02:00
parent ffc80b2c0d
commit 76500ebf15
3 changed files with 39 additions and 7 deletions

View File

@ -1,4 +1,5 @@
#import "GBHapticManager.h"
#import "GBHapticManagerLegacy.h"
#import <CoreHaptics/CoreHaptics.h>
@implementation GBHapticManager
@ -25,15 +26,14 @@
{
self = [super init];
if (!self) return nil;
if (self.class != [GBHapticManager class]) return self;
if (@available(iOS 13.0, *)) {
_engine = [[CHHapticEngine alloc] initAndReturnError:nil];
_engine.playsHapticsOnly = true;
_engine.autoShutdownEnabled = true;
_engine = [[CHHapticEngine alloc] initAndReturnError:nil];
_engine.playsHapticsOnly = true;
_engine.autoShutdownEnabled = true;
}
else {
return nil;
}
if (!_engine) return nil;
if (!_engine) return [[GBHapticManagerLegacy alloc] init];
return self;
}

View File

@ -0,0 +1,5 @@
#import "GBHapticManager.h"
@interface GBHapticManagerLegacy : GBHapticManager
@end

View File

@ -0,0 +1,27 @@
#import "GBHapticManagerLegacy.h"
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@implementation GBHapticManagerLegacy
- (void)doTapHaptic
{
[[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium] impactOccurred];
}
- (void)setRumbleStrength:(double)rumble
{
void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID);
void AudioServicesPlaySystemSoundWithVibration(SystemSoundID inSystemSoundID, id arg, NSDictionary* vibratePattern);
if (rumble) {
AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, @{@"Intensity": @(rumble),
@"OffDuration": @0,
@"OnDuration": @100,
@"VibePattern": @[@YES, @1000],
});
}
else {
AudioServicesStopSystemSound(kSystemSoundID_Vibrate);
}
}
@end