音频波动动效

一时失言乱红尘 2022-05-23 09:10 307阅读 0赞

效果 如下:

70

.h

#import

@interface PAAudioAnimationView : UIView

- (void)startAnimation;

- (void)stopAnimation;

- (void)setSoundsValue:(CGFloat)value;

@end

.m

#import “PAAudioAnimationView.h”

@interfacePAAudioAnimationView()

@property (nonatomic, assign) CGFloat itemLineWidth;

@property (nonatomic, assign) CGFloat itemLineSeparate;

@property (nonatomic, strong) UIColor *itemColor;

@property (nonatomic, strong) UIColor *itemBackColor;

@property (nonatomic, strong) CADisplayLink *displayLink;

@property (nonatomic, assign) CGFloat soundsValue;

@end

@implementation PAAudioAnimationView

- (instancetype)init{

  1. if (self = \[super init\]) \{
  2. \_itemLineWidth = 3;
  3. \_itemLineSeparate = 4;
  4. \_soundsValue = 0;
  5. \_itemColor = \[UIColorcolorWithRed:255/255.0green:96/255.0blue:0/255.0alpha:1/1.0\];
  6. \_itemBackColor = \[UIColorcolorWithRed:230/255.0green:230/255.0blue:230/255.0alpha:1/1.0\];
  7. \}
  8. returnself;

}

- (void)setFrame:(CGRect)frame{

  1. \[super setFrame:frame\];
  2. \[selfsetItems\];
  3. \[selfstopLayer\];

}

- (void)setSoundsValue:(CGFloat)value{

  1. \_soundsValue = value;

}

- (void)setItems{

  1. \[selfremoveAllSubLayers\];
  2. CGFloat width = self.frame.size.width;
  3. NSInteger numberItem = (width - self.itemLineWidth) / (\_itemLineWidth \+ \_itemLineSeparate);
  4. CGFloat layer\_w = \_itemLineWidth;
  5. CGFloat layer\_x = \_itemLineSeparate;
  6. CGFloat layer\_h = self.frame.size.height;
  7. CGFloat layer\_y = self.frame.size.height \- layer\_h;
  8. CGFloat min\_x = (width - 48 \- 34 \* 2) / 2;
  9. CGFloat max\_x = (width - (width - 48 \- 34 \* 2) / 2);
  10. for (int i = 0; i < numberItem; i++) \{
  11. layer\_x = \_itemLineSeparate \+ (\_itemLineSeparate \+ \_itemLineWidth) \* i;
  12. if (layer\_x > min\_x && layer\_x < max\_x) \{
  13. continue;
  14. \}
  15. layer\_y = \[self layer\_y:i\];
  16. layer\_h = \[self layer\_h:layer\_y\];
  17. UIBezierPath \*itemLinePath = \[UIBezierPathbezierPath\];
  18. \[itemLinePath moveToPoint:CGPointMake(0, layer\_y)\];
  19. \[itemLinePath addLineToPoint:CGPointMake(0, layer\_y + layer\_h)\];
  20. CAShapeLayer \*itemline = \[CAShapeLayerlayer\];
  21. itemline.lineCap = kCALineCapButt;
  22. itemline.lineJoin = kCALineJoinRound;
  23. \[itemline setLineWidth:layer\_w \* 2\];
  24. itemline.strokeColor = \[self.itemColor CGColor\];
  25. itemline.path = \[itemLinePath CGPath\];
  26. UIView \*dynamicView = \[\[UIView alloc\]initWithFrame:CGRectMake(layer\_x, layer\_y, layer\_w, layer\_h)\];
  27. dynamicView.backgroundColor = self.itemBackColor;
  28. dynamicView.layer.masksToBounds = YES;
  29. dynamicView.layer.cornerRadius = layer\_w / 2;
  30. dynamicView.tag = 111;
  31. \[dynamicView.layer addSublayer:itemline\];
  32. \[self addSubview:dynamicView\];
  33. \}

}

- (CGFloat)layer_y:(int)i{

  1. CGFloat layer\_y = 0;
  2. if (i % 4 == 0) \{
  3. layer\_y = (self.height \- self.height \* 2 / 4) / 2;
  4. \}else if (i % 4 == 1)\{
  5. layer\_y = (self.height \- self.height \* 3 / 4) / 2;
  6. \}else if (i % 4 == 2)\{
  7. layer\_y = 0;
  8. \}else\{
  9. layer\_y = (self.height \- self.height \* 3 / 4) / 2;;
  10. \}
  11. return layer\_y;

}

- (CGFloat)layer_h:(CGFloat)layer_y{

  1. return (self.height \- layer\_y \* 2);

}

- (void)removeAllSubLayers{

  1. for (UIView \*view in self.subviews) \{
  2. \[view removeFromSuperview\];
  3. \}

}

- (void)updateItemLevel{

  1. NSArray \*subViews = self.subviews;
  2. for (int i = 0; i < subViews.count; i++) \{
  3. UIView \*subView = subViews\[i\];
  4. if (subView.tag == 111) \{
  5. NSArray \*layers = subView.layer.sublayers;
  6. CAShapeLayer \*lineItem = (CAShapeLayer\*)layers.firstObject;
  7. CGFloat height = arc4random() % (NSInteger)self.frame.size.height \* 2;
  8. height \*= self.soundsValue;
  9. CGFloat value = (1 \- height \* 1.0 / self.frame.size.height);
  10. value = value < 0 ? 0 : value;
  11. lineItem.strokeStart = value;
  12. lineItem.strokeEnd = 1;
  13. \}
  14. \}

}

- (void)stopLayer{

  1. NSArray \*subViews = self.subviews;
  2. for (int i = 0; i < subViews.count; i++) \{
  3. UIView \*subView = subViews\[i\];
  4. if (subView.tag == 111) \{
  5. NSArray \*layers = subView.layer.sublayers;
  6. CAShapeLayer \*lineItem = (CAShapeLayer\*)layers.firstObject;
  7. lineItem.strokeStart = 1;
  8. lineItem.strokeEnd = 1;
  9. \}
  10. \}

}

#pragma mark - displayLink 定时器 动画

- (CADisplayLink *)displayLink{

  1. if (!\_displayLink) \{
  2. \_displayLink = \[CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(start)\];
  3. \}
  4. return\_displayLink;

}

- (void)start{

  1. \[selfupdateItemLevel\];

}

- (void)startAnimation{

  1. self.displayLink.frameInterval = 6;
  2. \[self.displayLinkaddToRunLoop:\[NSRunLoopcurrentRunLoop\] forMode:NSRunLoopCommonModes\];

}

- (void)stopAnimation{

  1. if (self.displayLink) \{
  2. \[self.displayLink invalidate\];
  3. self.displayLink = nil;
  4. \[self stopLayer\];
  5. \}

}

@end

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读