iOS实现音频进度条效果

前端之家收集整理的这篇文章主要介绍了iOS实现音频进度条效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

话不多说先上效果

iOS实现音频进度条效果

看到这个效果的时候我感觉相对比较难的点有两点:

一、是这个进度条的进度颜色变化,这里思路还是比较清晰的,直接用layer的mask来做就可以。

二、第二点就是这个各各条条的高度不一致又没有规律可言,在各个方法中我最终选择用随机数来做。

  好了思路清晰了,那就开始撸代码了。

首先创建一个View CYXAudioProgressView

  1. @interface CYXAudioProgressView : UIView
  2. //无动画设置 进度
  3. @property (assign,nonatomic) CGFloat persentage;
  4. //有动画设置 进度 0~1
  5. -(void)setAnimationPersentage:(CGFloat)persentage;
  6. /**
  7. 初始化layer 在完成frame赋值后调用一下
  8. */
  9. -(void)initLayers;
  10. @end

成员变量及初始化方法

  1. /*条条间隙*/
  2. #define kDrawMargin 4
  3. #define kDrawLineWidth 8
  4. /*差值*/
  5. #define differenceValue 51
  6. @interface CYXAudioProgressView ()<CAAnimationDelegate>
  7.  
  8. /*条条 灰色路径*/
  9. @property (nonatomic,strong) CAShapeLayer *shapeLayer;
  10. /*背景黄色*/
  11. @property (nonatomic,strong) CAShapeLayer *backColorLayer;
  12. @property (nonatomic,strong) CAShapeLayer *maskLayer;
  13. @end
  14. @implementation CYXAudioProgressView
  15.  
  16. -(instancetype)initWithFrame:(CGRect)frame{
  17. if (self = [super initWithFrame:frame]) {
  18. self.backgroundColor = [UIColor blackColor];
  19. [self.layer addSublayer:self.shapeLayer];
  20. [self.layer addSublayer:self.backColorLayer];
  21. self.persentage = 0.0;
  22. }
  23. return self;
  24. }

画图方法

  1. /**
  2. 初始化layer 在完成frame赋值后调用一下
  3. */
  4. -(void)initLayers{
  5. [self initStrokeLayer];
  6. [self setBackColorLayer];
  7. }

绘制路径

  1. /*路径*/
  2. -(void)initStrokeLayer{
  3. UIBezierPath *path = [UIBezierPath bezierPath];
  4. CGFloat maxWidth = self.frame.size.width;
  5. CGFloat drawHeight = self.frame.size.height;
  6. CGFloat x = 0.0;
  7. while (x+kDrawLineWidth<=maxWidth) {
  8. CGFloat random =5+ arc4random()%differenceValue;//差值在1-50 之间取
  9. NSLog(@"%f",random);
  10. [path moveToPoint:CGPointMake(x-kDrawLineWidth/2,random)];
  11. [path addLineToPoint:CGPointMake(x-kDrawLineWidth/2,drawHeight-random)];
  12. x+=kDrawLineWidth;
  13. x+=kDrawMargin;
  14. }
  15. self.shapeLayer.path = path.CGPath;
  16. self.backColorLayer.path = path.CGPath;
  17. }

设置mask来显示黄色路径

  1. /*设置masklayer*/
  2. -(void)setBackColorLayer{
  3. UIBezierPath *path = [UIBezierPath bezierPath];
  4. [path moveToPoint:CGPointMake(0,self.frame.size.height/2)];
  5. [path addLineToPoint:CGPointMake(self.frame.size.width,self.frame.size.height/2)];
  6. self.maskLayer.frame = self.bounds;
  7. self.maskLayer.lineWidth = self.frame.size.width;
  8. self.maskLayer.path= path.CGPath;
  9. self.backColorLayer.mask = self.maskLayer;
  10. }

手动设置百分比的两个方法

  1. -(void)setAnimationPersentage:(CGFloat)persentage{
  2. CGFloat startPersentage = self.persentage;
  3. [self setPersentage:persentage];
  4.  
  5. CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  6. pathAnimation.duration = 1;
  7. pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaSEOut];
  8. pathAnimation.fromValue = [NSNumber numberWithFloat:startPersentage];
  9. pathAnimation.toValue = [NSNumber numberWithFloat:persentage];
  10. pathAnimation.autoreverses = NO;
  11. pathAnimation.delegate = self;
  12. [self.maskLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
  13. }
  14. /**
  15. * 在修改百分比的时候,修改遮罩的大小
  16. *
  17. * @param persentage 百分比
  18. */
  19. - (void)setPersentage:(CGFloat)persentage {
  20.  
  21. _persentage = persentage;
  22. self.maskLayer.strokeEnd = persentage;
  23. }

最终使用

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view,typically from a nib.
  4. self.view.backgroundColor = [UIColor whiteColor];
  5.  
  6. self.loopProgressView.frame =CGRectMake(0,100,self.view.frame.size.width,150);
  7. [self.loopProgressView initLayers];
  8. [self.view addSubview:self.loopProgressView];
  9. dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{
  10. [self.loopProgressView setAnimationPersentage:0.5];
  11. });
  12.  
  13. self.slider.frame = CGRectMake(30,self.view.frame.size.height-60,self.view.frame.size.width-30*2,20);
  14. [self.view addSubview:self.slider];
  15. }

以上就简单的实现了上述效果,有问题欢迎指教。

Demo: https://github.com/SionChen/CYXAudioProgressView

总结

以上所述是小编给大家介绍的iOS实现音频进度条效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持

猜你在找的iOS相关文章