修改聊天闪退

This commit is contained in:
启星
2025-12-06 16:55:26 +08:00
parent 9f8a36544b
commit 16c8869961
12 changed files with 288 additions and 15 deletions

View File

@@ -0,0 +1,172 @@
//
// QXQXQXHapticManager.m
// QXLive
//
// Created by on 2025/12/5.
//
#import "QXHapticManager.h"
#import <sys/utsname.h>
@interface QXHapticManager ()
@property (nonatomic, strong) UIImpactFeedbackGenerator *impactGenerator;
@property (nonatomic, strong) UINotificationFeedbackGenerator *notificationGenerator;
@property (nonatomic, strong) UISelectionFeedbackGenerator *selectionGenerator;
@property (nonatomic, assign) BOOL isImpactPrepared;
@property (nonatomic, assign) UIImpactFeedbackStyle preparedStyle;
@end
@implementation QXHapticManager
+ (instancetype)shared {
static QXHapticManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[QXHapticManager alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_isImpactPrepared = NO;
//
_impactGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
_notificationGenerator = [[UINotificationFeedbackGenerator alloc] init];
_selectionGenerator = [[UISelectionFeedbackGenerator alloc] init];
//
[self prepareAllGenerators];
}
return self;
}
#pragma mark -
+ (BOOL)isHapticFeedbackSupported {
if (@available(iOS 10.0, *)) {
//
NSString *deviceModel = [[UIDevice currentDevice] model];
// iPhone 7
// 使
// 使 sysctlbyname
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceIdentifier = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
// iPhone 7
NSArray *supportedDevices = @[
@"iPhone9,", @"iPhone10,", @"iPhone11,", @"iPhone12,",
@"iPhone13,", @"iPhone14,", @"iPhone15,", @"iPhone16,",
@"iPhone8,4" // iPhone SE ()
];
BOOL isSupportedDevice = NO;
for (NSString *prefix in supportedDevices) {
if ([deviceIdentifier hasPrefix:prefix]) {
isSupportedDevice = YES;
break;
}
}
// YES
#if TARGET_IPHONE_SIMULATOR
return YES;
#else
return isSupportedDevice;
#endif
}
return NO;
}
- (void)prepareAllGenerators {
if (![QXHapticManager isHapticFeedbackSupported]) {
return;
}
// 线
dispatch_async(dispatch_get_main_queue(), ^{
[self.impactGenerator prepare];
[self.notificationGenerator prepare];
[self.selectionGenerator prepare];
});
}
#pragma mark -
- (void)impactWithStyle:(UIImpactFeedbackStyle)style {
if (![QXHapticManager isHapticFeedbackSupported]) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
//
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:style];
[generator prepare];
[generator impactOccurred];
//
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[generator prepare];
});
});
}
- (void)impactMedium {
[self impactWithStyle:UIImpactFeedbackStyleMedium];
}
#pragma mark -
- (void)prepareImpactWithStyle:(UIImpactFeedbackStyle)style {
if (![QXHapticManager isHapticFeedbackSupported]) {
return;
}
self.preparedStyle = style;
dispatch_async(dispatch_get_main_queue(), ^{
//
self.impactGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:style];
[self.impactGenerator prepare];
self.isImpactPrepared = YES;
});
}
- (void)triggerPreparedImpact {
if (![QXHapticManager isHapticFeedbackSupported] || !self.isImpactPrepared) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
if (self.impactGenerator) {
[self.impactGenerator impactOccurred];
//
[self.impactGenerator prepare];
}
});
}
#pragma mark -
- (void)notificationWithType:(UINotificationFeedbackType)type {
if (![QXHapticManager isHapticFeedbackSupported]) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.notificationGenerator notificationOccurred:type];
[self.notificationGenerator prepare];
});
}
- (void)selectionChanged {
if (![QXHapticManager isHapticFeedbackSupported]) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.selectionGenerator selectionChanged];
[self.selectionGenerator prepare];
});
}
@end