123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //
- // KSSpectrumView.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2024/7/29.
- //
- #import "KSSpectrumView.h"
- @interface KSSpectrumView ()
- @property (nonatomic, strong) CAGradientLayer *combinedGradientLayer;
- @end
- @implementation KSSpectrumView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self setupView];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)aDecoder {
- self = [super initWithCoder:aDecoder];
- if (self) {
- [self setupView];
- }
- return self;
- }
- - (void)setupView {
- self.barWidth = 3.0;
- self.space = 1.0;
- self.combinedGradientLayer = [CAGradientLayer layer];
- self.combinedGradientLayer.colors = @[
- (__bridge id)HexRGBAlpha(0xffffff, 0.32f).CGColor,
- (__bridge id)HexRGBAlpha(0xffffff, 0.32f).CGColor
- ];
- self.combinedGradientLayer.locations = @[@0.6, @1.0];
- [self.layer addSublayer:self.combinedGradientLayer];
- }
- - (void)resetLayer {
- self.spectra = [NSArray array];
- }
- - (void)setSpectra:(NSArray<NSArray<NSNumber *> *> *)spectra {
- _spectra = spectra;
- if (spectra) {
- NSUInteger spectraCount = [spectra[0] count];
- NSMutableArray<NSNumber *> *combinedSpectrum = [NSMutableArray arrayWithCapacity:spectraCount];
- // 取两个声道数据中的最大值
- for (NSUInteger i = 0; i < spectraCount; i++) {
- NSNumber *leftAmplitude = spectra[0][i];
- NSNumber *rightAmplitude = spectra.count > 1 ? spectra[1][i] : @0;
- CGFloat maxAmplitude = MAX(leftAmplitude.floatValue, rightAmplitude.floatValue);
- [combinedSpectrum addObject:@(maxAmplitude)];
- }
-
- CGFloat viewHeight = self.bounds.size.height;
- CGFloat viewWidth = self.bounds.size.width;
- CGFloat middleY = viewHeight / 2.0;
- CGFloat barHeight = (viewHeight) / 2.0;
- CGFloat cornerRadius = viewWidth / 2.0f;
- CGFloat xIncrement = self.barWidth + self.space;
- UIBezierPath *combinedPath = [UIBezierPath bezierPath];
- // Left channel
- for (NSUInteger i = 0; i < spectraCount; i++) {
- CGFloat x = i * xIncrement + self.space;
- CGFloat amplitudeValue = combinedSpectrum[i].floatValue;
- CGFloat height = amplitudeValue * barHeight;
- CGFloat y = middleY - height/2.0; // Centered vertically
-
- CGRect rect = CGRectMake(x, y, self.barWidth, height);
- UIBezierPath *barPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
- [combinedPath appendPath:barPath];
- }
-
- CAShapeLayer *combinedMaskLayer = [CAShapeLayer layer];
- combinedMaskLayer.path = combinedPath.CGPath;
- self.combinedGradientLayer.frame = CGRectMake(0, 0, viewWidth, viewHeight);
- self.combinedGradientLayer.mask = combinedMaskLayer;
- }
- }
- - (CGFloat)translateAmplitudeToYPosition:(float)amplitude {
- CGFloat barHeight = amplitude * self.bounds.size.height;
- return self.bounds.size.height - barHeight;
- }
- - (void)dealloc {
- NSLog(@"---- KSSpectrumView dealloc");
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|