面试题答案
一键面试1. 引入Core Haptics框架
在项目的.h
文件或.m
文件开头引入Core Haptics框架:
#import <CoreHaptics/CoreHaptics.h>
2. 数据结构设计
- 音符结构体:用于表示单个音符及其对应的触觉反馈属性。
typedef struct {
float intensity; // 触觉反馈强度,范围0.0 - 1.0
float duration; // 触觉反馈时长,单位秒
} NoteHaptic;
typedef struct {
NoteHaptic notes[128]; // 假设最多支持128种不同音符
} MusicHapticData;
- 节奏结构体:用于表示节奏模式以及对应音符的触发顺序。
typedef struct {
NSInteger noteIndices[100]; // 假设一个节奏最多100个音符
NSInteger count;
} RhythmPattern;
3. 初始化Core Haptics引擎
在合适的地方,比如视图控制器的viewDidLoad
方法中初始化Core Haptics引擎:
- (void)viewDidLoad {
[super viewDidLoad];
if (@available(iOS 13.0, *)) {
CHHapticEngine *engine = [[CHHapticEngine alloc] initWithCallbackQueue:dispatch_get_main_queue()];
NSError *error;
if (![engine startAndReturnError:&error]) {
NSLog(@"Error starting haptic engine: %@", error);
}
self.hapticEngine = engine;
}
}
4. 规划反馈序列
- 根据音符创建触觉事件:
- (CHHapticEvent *)hapticEventForNoteIndex:(NSInteger)noteIndex withData:(MusicHapticData *)data {
if (@available(iOS 13.0, *)) {
NoteHaptic note = data->notes[noteIndex];
CHHapticEventParameter *intensityParam = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:note.intensity relative:NO];
CHHapticEventParameter *durationParam = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticDuration value:note.duration relative:NO];
CHHapticEvent *event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:@[intensityParam, durationParam] relativeTime:0];
return event;
}
return nil;
}
- 根据节奏模式创建反馈序列:
- (CHHapticPattern *)hapticPatternForRhythm:(RhythmPattern *)rhythm withData:(MusicHapticData *)data {
if (@available(iOS 13.0, *)) {
NSMutableArray<CHHapticEvent *> *events = [NSMutableArray array];
float timeOffset = 0;
for (NSInteger i = 0; i < rhythm.count; i++) {
NSInteger noteIndex = rhythm.noteIndices[i];
CHHapticEvent *event = [self hapticEventForNoteIndex:noteIndex withData:data];
event.relativeTime = timeOffset;
[events addObject:event];
timeOffset += data->notes[noteIndex].duration;
}
CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:events error:nil];
return pattern;
}
return nil;
}
5. 触发触觉反馈
当需要播放与音乐对应的触觉反馈时,执行以下操作:
- (void)playHapticForRhythm:(RhythmPattern *)rhythm withData:(MusicHapticData *)data {
if (@available(iOS 13.0, *)) {
CHHapticPattern *pattern = [self hapticPatternForRhythm:rhythm withData:data];
CHHapticPatternPlayer *player = [self.hapticEngine makePlayerWithPattern:pattern error:nil];
[player startAtTime:0 error:nil];
}
}
6. 清理资源
在适当的时候,比如视图控制器销毁时,停止并关闭Core Haptics引擎:
- (void)dealloc {
if (@available(iOS 13.0, *)) {
[self.hapticEngine stopAndReturnError:nil];
[self.hapticEngine close];
}
}