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檢視