您好,登錄后才能下訂單哦!
前幾天開發群里有一個老兄問了一個開發問題,他們的需求是要做一個類似音頻進度條的東西,我感覺設計還不錯,于是就寫了個小demo供大家參考,在爭得了他的同意的情況下寫下這篇文章。
話不多說先上效果圖
看到這個效果的時候我感覺相對比較難的點有兩點:
一、是這個進度條的進度顏色變化,這里思路還是比較清晰的,直接用layer的mask來做就可以。
二、第二點就是這個各各條條的高度不一致又沒有規律可言,在各個方法中我最終選擇用隨機數來做。
好了思路清晰了,那就開始擼代碼了。
首先創建一個View CYXAudioProgressView
@interface CYXAudioProgressView : UIView //無動畫設置 進度 @property (assign, nonatomic) CGFloat persentage; //有動畫設置 進度 0~1 -(void)setAnimationPersentage:(CGFloat)persentage; /** 初始化layer 在完成frame賦值后調用一下 */ -(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賦值后調用一下 */ -(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; }
設置mask來顯示黃色路徑
/*設置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; }
手動設置百分比的兩個方法
-(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]; }
以上就簡單的實現了上述效果,有問題歡迎指教。
Demo: https://github.com/SionChen/CYXAudioProgressView
總結
以上所述是小編給大家介紹的iOS實現音頻進度條效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。