前几天开发群里有一个老兄问了一个开发问题,他们的需求是要做一个类似音频进度条的东西,我感觉设计还不错,于是就写了个小demo供大家参考,在争得了他的同意的情况下写下这篇文章。
话不多说先上效果图

看到这个效果的时候我感觉相对比较难的点有两点:
一、是这个进度条的进度颜色变化,这里思路还是比较清晰的,直接用 的mask来做就可以。
二、第二点就是这个各各条条的高度不一致又没有规律可言,在各个方法中我最终选择用随机数来做。
好了思路清晰了,那就开始撸代码了。
首先创建一个View CYXAudioProgressView
@interface CYXAudioProgressView : UIView //无动画设置 进度 @property (assign, nonatomic) CGFloat persentage; //有动画设置 进度 0~1 -(void)setAnimationPersentage:(CGFloat)persentage; /** 初始化 在完成 赋值后调用一下 */ -(void)init s; @end
成员变量及初始化方法
/*条条间隙*/
#define kDrawMargin 4
#define kDrawLineWidth 8
/*差值*/
#define differenceValue 51
@interface CYXAudioProgressView ()<CAAnimationDelegate>
/*条条 灰色路径*/
@property (nonatomic,strong) CAShape *shape ;
/*背景黄色*/
@property (nonatomic,strong) CAShape *backColor ;
@property (nonatomic,strong) CAShape *mask ;
@end
@implementation CYXAudioProgressView
-(instancetype)initWith :(CGRect) {
if (self = [super initWith : ]) {
self.backgroundColor = [UIColor blackColor];
[self. addSub :self.shape ];
[self. addSub :self.backColor ];
self.persentage = 0.0;
}
return self;
}
画图方法:
/**
初始化 在完成 赋值后调用一下
*/
-(void)init s{
[self initStroke ];
[self setBackColor ];
}
绘制路径
/*路径*/
-(void)initStroke {
UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat maxWidth = self. .size.width;
CGFloat drawHeight = self. .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.shape .path = path.CGPath;
self.backColor .path = path.CGPath;
}
设置mask来显示黄色路径
/*设置mask */
-(void)setBackColor {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, self. .size.height/2)];
[path addLineToPoint:CGPointMake(self. .size.width, self. .size.height/2)];
self.mask . = self.bounds;
self.mask .lineWidth = self. .size.width;
self.mask .path= path.CGPath;
self.backColor .mask = self.mask ;
}
手动设置百分比的两个方法
-(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.mask addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}
/**
* 在修改百分比的时候,修改遮罩的大小
*
* @param persentage 百分比
*/
- (void)setPersentage:(CGFloat)persentage {
_persentage = persentage;
self.mask .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. =CGRectMake(0, 100, self.view. .size.width, 150);
[self.loopProgressView init s];
[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. = CGRectMake(30, self.view. .size.height-60, self.view. .size.width-30*2, 20);
[self.view addSubview:self.slider];
}
以上就简单的实现了上述效果,有问题欢迎指教。
Demo: https://github.com/SionChen/CYXAudioProgressView
总结
以上所述是小编给大家介绍的iOS实现音频进度条效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
继续阅读与本文标签相同的文章
-
美国SpaceX公司计划向太空发射4.2万枚通信卫星
2026-05-18栏目: 教程
-
这几个小程序,让你的生活质量提高30%
2026-05-18栏目: 教程
-
超赞的二次识图精要讲解
2026-05-18栏目: 教程
-
为何如今很少看到电脑病毒?专家道出3点原因,现在知道不算晚
2026-05-18栏目: 教程
-
雷诺与Waymo将合作开发自动驾驶路线
2026-05-18栏目: 教程
