iOS實(shí)現(xiàn)音頻進(jìn)度條效果
前幾天開發(fā)群里有一個老兄問了一個開發(fā)問題,他們的需求是要做一個類似音頻進(jìn)度條的東西,我感覺設(shè)計(jì)還不錯,于是就寫了個小demo供大家參考,在爭得了他的同意的情況下寫下這篇文章。
話不多說先上效果圖
看到這個效果的時候我感覺相對比較難的點(diǎn)有兩點(diǎn):
一、是這個進(jìn)度條的進(jìn)度顏色變化,這里思路還是比較清晰的,直接用layer的mask來做就可以。
二、第二點(diǎn)就是這個各各條條的高度不一致又沒有規(guī)律可言,在各個方法中我最終選擇用隨機(jī)數(shù)來做。
好了思路清晰了,那就開始擼代碼了。
首先創(chuàng)建一個View CYXAudioProgressView
@interface CYXAudioProgressView : UIView //無動畫設(shè)置 進(jìn)度 @property (assign, nonatomic) CGFloat persentage; //有動畫設(shè)置 進(jìn)度 0~1 -(void)setAnimationPersentage:(CGFloat)persentage; /** 初始化layer 在完成frame賦值后調(diào)用一下 */ -(void)initLayers; @end
成員變量及初始化方法
/*條條間隙*/ #define kDrawMargin 4 #define kDrawLineWidth 8 /*差值*/ #define differenceValue 51 @interface CYXAudioProgressView ()<CAAnimationDelegate> /*條條 灰色路徑*/ @property (nonatomic,strong) CAShapeLayer *shapeLayer; /*背景黃色*/ @property (nonatomic,strong) CAShapeLayer *backColorLayer; @property (nonatomic,strong) CAShapeLayer *maskLayer; @end @implementation CYXAudioProgressView -(instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor blackColor]; [self.layer addSublayer:self.shapeLayer]; [self.layer addSublayer:self.backColorLayer]; self.persentage = 0.0; } return self; }
畫圖方法:
/** 初始化layer 在完成frame賦值后調(diào)用一下 */ -(void)initLayers{ [self initStrokeLayer]; [self setBackColorLayer]; }
繪制路徑
/*路徑*/ -(void)initStrokeLayer{ UIBezierPath *path = [UIBezierPath bezierPath]; CGFloat maxWidth = self.frame.size.width; CGFloat drawHeight = self.frame.size.height; CGFloat x = 0.0; while (x+kDrawLineWidth<=maxWidth) { CGFloat random =5+ arc4random()%differenceValue;//差值在1-50 之間取 NSLog(@"%f",random); [path moveToPoint:CGPointMake(x-kDrawLineWidth/2, random)]; [path addLineToPoint:CGPointMake(x-kDrawLineWidth/2, drawHeight-random)]; x+=kDrawLineWidth; x+=kDrawMargin; } self.shapeLayer.path = path.CGPath; self.backColorLayer.path = path.CGPath; }
設(shè)置mask來顯示黃色路徑
/*設(shè)置masklayer*/ -(void)setBackColorLayer{ UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0, self.frame.size.height/2)]; [path addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height/2)]; self.maskLayer.frame = self.bounds; self.maskLayer.lineWidth = self.frame.size.width; self.maskLayer.path= path.CGPath; self.backColorLayer.mask = self.maskLayer; }
手動設(shè)置百分比的兩個方法
-(void)setAnimationPersentage:(CGFloat)persentage{ CGFloat startPersentage = self.persentage; [self setPersentage:persentage]; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; pathAnimation.duration = 1; pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; pathAnimation.fromValue = [NSNumber numberWithFloat:startPersentage]; pathAnimation.toValue = [NSNumber numberWithFloat:persentage]; pathAnimation.autoreverses = NO; pathAnimation.delegate = self; [self.maskLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; } /** * 在修改百分比的時候,修改遮罩的大小 * * @param persentage 百分比 */ - (void)setPersentage:(CGFloat)persentage { _persentage = persentage; self.maskLayer.strokeEnd = persentage; }
最終使用
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.loopProgressView.frame =CGRectMake(0, 100, self.view.frame.size.width, 150); [self.loopProgressView initLayers]; [self.view addSubview:self.loopProgressView]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.loopProgressView setAnimationPersentage:0.5]; }); self.slider.frame = CGRectMake(30, self.view.frame.size.height-60, self.view.frame.size.width-30*2, 20); [self.view addSubview:self.slider]; }
以上就簡單的實(shí)現(xiàn)了上述效果,有問題歡迎指教。
Demo: https://github.com/SionChen/CYXAudioProgressView
總結(jié)
以上所述是小編給大家介紹的iOS實(shí)現(xiàn)音頻進(jìn)度條效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- iOS中使用NSProgress類來創(chuàng)建UI進(jìn)度條的方法詳解
- IOS實(shí)現(xiàn)簡單的進(jìn)度條功能
- iOS快速實(shí)現(xiàn)環(huán)形漸變進(jìn)度條
- iOS 進(jìn)度條、加載、安裝動畫的簡單實(shí)現(xiàn)
- ios開發(fā)加載webview顯示進(jìn)度條實(shí)例
- iOS實(shí)現(xiàn)帶動畫的環(huán)形進(jìn)度條
- 使用axios實(shí)現(xiàn)上傳圖片進(jìn)度條功能
- Android仿IOS ViewPager滑動進(jìn)度條
- iOS中利用CoreAnimation實(shí)現(xiàn)一個時間的進(jìn)度條效果
- iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼
相關(guān)文章
總結(jié)IOS關(guān)閉鍵盤/退出鍵盤的五種方式
IOS開發(fā)中經(jīng)常要用到輸入框,默認(rèn)情況下點(diǎn)擊輸入框就會彈出鍵盤,但是必須要實(shí)現(xiàn)輸入框return的委托方法才能取消鍵盤的顯示,對于用戶體驗(yàn)來說很不友好,我們可以實(shí)現(xiàn)例如點(diǎn)擊鍵盤以外的空白區(qū)域來將鍵盤關(guān)閉的功能,以下是我總結(jié)出的幾種關(guān)閉鍵盤的方法。2016-08-08IOS開發(fā)中異步網(wǎng)絡(luò)請求上實(shí)現(xiàn)同步邏輯
這篇文章主要介紹了IOS開發(fā)中異步網(wǎng)絡(luò)請求上實(shí)現(xiàn)同步邏輯的相關(guān)資料,需要的朋友可以參考下2017-03-03ios uicollectionview實(shí)現(xiàn)橫向滾動
這篇文章主要為大家詳細(xì)介紹了ios uicollectionview實(shí)現(xiàn)橫向滾動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03IOS 開發(fā)APP之關(guān)于時間處理詳細(xì)介紹
這篇文章主要介紹了IOS 開發(fā)APP之關(guān)于時間處理詳細(xì)介紹的相關(guān)資料,開發(fā)APP 不僅需要對API的調(diào)用還需要對時間相關(guān)的各種API之間的差別,再因場景而異去設(shè)計(jì)相應(yīng)的機(jī)制,需要的朋友可以參考下2016-12-12ios UITableView 自定義右滑刪除的實(shí)現(xiàn)代碼
這篇文章主要介紹了ios UITableView 自定義右滑刪除的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07IOS開發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時功能
在制作IOS項(xiàng)目中,我們經(jīng)常要用到倒計(jì)時功能,今天就分享下使用nstimer實(shí)現(xiàn)的倒計(jì)時功能的代碼,希望對大家能有所幫助2014-09-09iOS mobileconfig配置文件進(jìn)行簽名的配置方法
這篇文章主要介紹了iOS mobileconfig配置文件進(jìn)行簽名的配置方法,給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02