Demo&效果图
我将节拍器View部分分成了四部分,负责处理各自操作。
View
HLRhythmSpotView
负责展示节拍数,会根据节拍显示闪烁动画
HLChoiceRhythmView
负责切换节拍数
HLChangeSpeedView
负责切换频率
HLPlaySwitchView
负责播放、暂停、切换频率
节拍管理类
HLMetronomeAudioManager
负责管理节拍的播放、暂停、频率
typedef NS_ENUM(NSInteger, HLMetronomeType) {
HLMetronomeType1V4 =0, // 1/4
HLMetronomeType2V4, // 2/4
HLMetronomeType3V4, // 3/4
HLMetronomeType4V4, // 4/4
HLMetronomeType3V8, // 3/8
HLMetronomeType6V8 // 6/8
};
// 代理
@protocol HLMetronomeAudioManagerDelegate <NSObject>
/** 当前播放音频的总次数(用来处理上部圆View那个显示高亮状态) */
- (void)hlMetronomeAudioCurrentTotalNo:(int)currentTotalNo;
@end
@interface HLMetronomeAudioManager : NSObject
+ (instancetype)sharedAudioManager;
/** 代理 */
@property (nonatomic, weak) id <HLMetronomeAudioManagerDelegate> delegate;
/** 节拍类型 */
@property (nonatomic, assign) HLMetronomeType metronomeStat;
/** 播放速率,范围40~240 */
@property (nonatomic, assign) int rate;
/** 播放 */
- (void)play;
/** 暂停 */
- (void)pause;
@end
每个View
通过代理将用户操作事件传回HLMetronomeViewController
,由HLMetronomeViewController
调用HLMetronomeAudioManager
的方法操作节拍音频。
#pragma mark -- 初始化节拍器播放器
- (void)initMetronomeAudio{
[HLMetronomeAudioManager sharedAudioManager].delegate = self;
[[HLMetronomeAudioManager sharedAudioManager] setMetronomeStat:HLMetronomeType1V4];
[[HLMetronomeAudioManager sharedAudioManager] setRate:40];
[self.rhythmSpotView updateSpotView:HLMetronomeType1V4];
}
#pragma mark -- HLChoiceRhythmViewDelegate
- (void)hlChangeRhythm:(HLMetronomeType)metronomeStat{
[self.rhythmSpotView updateSpotView:metronomeStat];
[[HLMetronomeAudioManager sharedAudioManager] setMetronomeStat:metronomeStat];
}
#pragma mark -- HLChangeSpeedViewDelegate
- (void)hlChangeSpeed:(int)speed{
[[HLMetronomeAudioManager sharedAudioManager] setRate:speed];
}
#pragma mark -- HLPlaySwitchViewDelegate
- (void)hlPlay{
[[HLMetronomeAudioManager sharedAudioManager] play];
}
- (void)hlPause{
[[HLMetronomeAudioManager sharedAudioManager] pause];
}
- (void)hlChangeSpeedWithIsAdd:(BOOL)isAdd speed:(int)speed{
[self.changeSpeedView changeSpeedWithIsAdd:isAdd speed:speed];
}
#pragma mark -- HLMetronomeAudioManagerDelegate
- (void)hlMetronomeAudioCurrentTotalNo:(int)currentTotalNo{
[self.rhythmSpotView updateSpotViewHeightState:currentTotalNo];
}
具体代码可以下载Demo查看