Demo&效果圖
我將節(jié)拍器View部分分成了四部分猎贴,負責處理各自操作宫屠。
View
HLRhythmSpotView
負責展示節(jié)拍數(shù)黑低,會根據(jù)節(jié)拍顯示閃爍動畫
HLChoiceRhythmView
負責切換節(jié)拍數(shù)
HLChangeSpeedView
負責切換頻率
HLPlaySwitchView
負責播放鳍侣、暫停、切換頻率
節(jié)拍管理類
HLMetronomeAudioManager
負責管理節(jié)拍的播放应民、暫停话原、頻率
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>
/** 當前播放音頻的總次數(shù)(用來處理上部圓View那個顯示高亮狀態(tài)) */
- (void)hlMetronomeAudioCurrentTotalNo:(int)currentTotalNo;
@end
@interface HLMetronomeAudioManager : NSObject
+ (instancetype)sharedAudioManager;
/** 代理 */
@property (nonatomic, weak) id <HLMetronomeAudioManagerDelegate> delegate;
/** 節(jié)拍類型 */
@property (nonatomic, assign) HLMetronomeType metronomeStat;
/** 播放速率夕吻,范圍40~240 */
@property (nonatomic, assign) int rate;
/** 播放 */
- (void)play;
/** 暫停 */
- (void)pause;
@end
每個View
通過代理將用戶操作事件傳回HLMetronomeViewController
,由HLMetronomeViewController
調(diào)用HLMetronomeAudioManager
的方法操作節(jié)拍音頻繁仁。
#pragma mark -- 初始化節(jié)拍器播放器
- (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查看