您好,登錄后才能下訂單哦!
iOS中怎么實現(xiàn)音頻進度條效果,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一、是這個進度條的進度顏色變化,這里思路還是比較清晰的,直接用layer的mask來做就可以。
二、第二點就是這個各各條條的高度不一致又沒有規(guī)律可言,在各個方法中我最終選擇用隨機數(shù)來做。
??好了思路清晰了,那就開始擼代碼了。
首先創(chuàng)建一個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];}
關于iOS中怎么實現(xiàn)音頻進度條效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。