1.需求概述
根據(jù)產(chǎn)品需求需要做進(jìn)度條上播放時間替換UISlider滑塊,因?yàn)轫?xiàng)目給的工期很短,很多代碼能省就省但百度谷歌未果之后決定自己寫。完成了如下需求:

2.基本實(shí)現(xiàn)思路:
? ? ? ? 已知:總播放時間totalT,當(dāng)前播放時間t
?? ? ? ? ? ? ? ? ? 視圖總寬度totalW
?? ? ? ? ? ? ? ? ? 播放顯示時長視圖寬度timeW
? ? ? ? 可計(jì)算出 :
?? ? ? ? ? ? ? ? ? 播放時間視圖移動總距離:totalW - timeW
?? ? ? ? ? ? ? ? ? 每個時間單位移動的距離:(totalW - timeW)/totalT? moveW
? ? ? ? 正常播放時:
?? ? ? ? ? ? ? ? ? 傳入當(dāng)前播放時間及總時間:t/60 + t%60 totalT/60 + totalT%60 直接顯示即可
?? ? ? ? ? ? ? ? ? 時間視圖位置x值改變:t*moveW
?? ? ? ? ? ? ? ? ? 播放結(jié)束判斷為: t = totalT
?? ? ? ? 移動滑塊(顯示時間視圖):doMoveAction:
?? ? ? ? ? ? ? ? ? 獲取移動距離 : moveW = newCenter.x - recognizer.view.frame.size.width/2;
?? ? ? ? ? ? ? ? ? 獲取移動多少時間:
?? ? ? ? ? ? ? ? ? ? ? ? ? 假設(shè):移動距離為10 ,總距離為100,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 移動距離占總距離為0.1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 總時間為300秒
?? ? ? ? ? ? ? ? ? ? ? ? ? 得出移動后的時間:0.1*300 = 30秒
?? ? ? ? ? ? ? ? ? 傳入播放器即可
3.主要功能代碼
```
?// Figure out where the user is trying to drag the view.
? ? CGPointtranslation = [recognizertranslationInView:self];
? ? CGPointnewCenter =CGPointMake(recognizer.view.center.x+ translation.x,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? recognizer.view.center.y+ translation.y);
? ? //? ? 限制屏幕范圍:
? ? newCenter.y=MAX(recognizer.view.frame.size.height/2, recognizer.view.frame.size.width/2);
? ? newCenter.y=MIN(self.frame.size.height - recognizer.view.frame.size.height/2, recognizer.view.frame.size.width/2);
? ? newCenter.x=MAX(recognizer.view.frame.size.width/2, newCenter.x);
? ? newCenter.x=MIN(self.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
? ? recognizer.view.center= newCenter;
? ? [recognizersetTranslation:CGPointZeroinView:self];
? ? CGRectgreenRect =self.backGreenView.frame;
? ? greenRect.size.width= newCenter.x;
? ? self.backGreenView.frame= greenRect;
? ? CGFloattotalW =self.tempFrame.size.width-self.playTimeW;
? ? CGFloatmoveW = newCenter.x- recognizer.view.frame.size.width/2;
? ? intmoveX = moveW/totalW*self.totalLength;
? ? if([self.delegaterespondsToSelector:@selector(changePlayTimeByPublicAudioPlayProgressView:)]) {
? ? ? ? [self.delegate changePlayTimeByPublicAudioPlayProgressView:moveX];
? ? }
```
PS:歷時四個小時完成這個功能。時間比較倉促,希望大神們多多提提意見。寫的不好的地方大神們多擔(dān)待多指教。
此需求Demo下載地址:GitHub下載地址