開篇
播放音頻的效果圖:
效果圖
需求分析
- 灰色波形底圖
- 播放過程,白色波形覆蓋
實現(xiàn)思路
灰色波形底圖是固定的莱坎,可直接用圖層繪制
1衣式、波形的路徑計算與上篇類似
//波形路徑
- (CGPathRef)pathWithPoints:(NSArray *)points{
CGFloat midY = NSHeight(self.bounds) / 2.f;
CGFloat leftX = NSMaxX(playBtnRect);
CGMutablePathRef wavePath = CGPathCreateMutable(); //繪制路徑
CGPathMoveToPoint(wavePath, nil, leftX, midY);
for (NSInteger i = 0; i < _pointArray.count; i++) {
NSValue *pointValue = _pointArray[i];
NSPoint point = pointValue.pointValue;
if (point.y == 0) {
CGPathMoveToPoint(wavePath, nil, leftX + i - 1, midY);
CGPathAddLineToPoint(wavePath, NULL, leftX + i, midY);
}else{
CGPathMoveToPoint(wavePath, nil, leftX + i, midY);
CGPathAddLineToPoint(wavePath, NULL, leftX + i, midY + point.y);
CGPathMoveToPoint(wavePath, nil, leftX + i, midY);
CGPathAddLineToPoint(wavePath, NULL, leftX + i, midY - point.y);
}
}
CGPathRef path = CGPathCreateCopy(wavePath);
CGPathRelease(wavePath);
return path;
}
繪制灰色波形
//添加完整波形圖層
- (void)addWaveLayerWithPath:(CGPathRef)wavePath{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.lineWidth=1;
shapeLayer.strokeColor=[NSColor lightGrayColor].CGColor;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.lineJoin = kCALineJoinRound;
[self.layer addSublayer:shapeLayer];
shapeLayer.path = wavePath;
}
2、播放圖層檐什,使用CAShapeLayer
實現(xiàn)碴卧,CAShapeLayer
是唯一一個可動畫效果的圖層了
//添加播放動畫圖層
- (void)addAnimationLayerWithPath:(CGPathRef)path{
animationLayer = [CAShapeLayer layer];
animationLayer.path = path;
animationLayer.lineWidth = 1;
animationLayer.strokeColor=[NSColor whiteColor].CGColor;
animationLayer.lineCap = kCALineCapRound;
animationLayer.lineJoin = kCALineJoinRound;
[self.layer addSublayer:animationLayer];
animationLayer.speed = 0; //禁止動畫執(zhí)行
animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = _playDuration;
animation.fromValue = @(0.0f);
animation.toValue = @(1.0f);
animation.delegate = self;
[animationLayer addAnimation:animation forKey:@""];
}
animationLayer.speed
設(shè)為0,即動畫速度為0乃正,也就是不執(zhí)行住册。
@"strokeEnd"
是路徑的結(jié)束點,始于0瓮具,止于1荧飞,當(dāng)動畫開始執(zhí)行,就能看到繪制過程了名党。
3叹阔、播放的開始、暫停传睹、繼續(xù)耳幢、停止
對動畫的控制,主要是對speed
欧啤、timeOffset
睛藻、beginTime
等屬性的設(shè)置启上。
- (void)play{
[self resume];
}
- (void)pause{
_playing = NO;
[self setNeedsDisplay:YES];
CFTimeInterval pausedTime = [animationLayer convertTime:CACurrentMediaTime() fromLayer:nil];
animationLayer.speed = 0;
animationLayer.timeOffset = pausedTime;
}
- (void)resume{
_playing = YES;
[self setNeedsDisplay:YES];
CFTimeInterval pausedTime = [animationLayer timeOffset];
animationLayer.speed = 1.0;
animationLayer.timeOffset = 0.0;
animationLayer.beginTime = 0;
CFTimeInterval timeSincePause = [animationLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
animationLayer.beginTime = timeSincePause;
}
- (void)stop{
_playing = NO;
[self setNeedsDisplay:YES];
animationLayer.timeOffset = 0;
animationLayer.speed = 0;
//動畫播放完成后,默認自動removed
[animationLayer addAnimation:animation forKey:@""];
}
Demo 地址:https://github.com/YunFei2015/AudioWaveAnimation.git