我有一个从音频文件的样本创建的波形的UIImage
。波形为蓝色,背景为白色。我希望蓝色的波形改变为红色的音频正在播放,以表明进度。如果我有两个相同波形的UIImage,一个是蓝色的,一个是红色的,我能做一些动画图像擦除来实现这个吗?
我已经能够实现这一点,当波形是一个UIView
,我只是画一个红色的UIView波形在我的蓝色UIView波形,但如果我有多个波形(我做-在一个表视图),这需要我的CPU使用率超过95%,并导致很多延迟-即使没有音频播放。我相信如果我走UIImage路线,事情会平滑得多,因为我只有静态图像。
提示吗?
据我所知,要做到这一点,唯一的方法是有两个UIImageViews一个在另一个的顶部,并动画蒙版在顶部,使其消失在一个从左到右的方式,从而揭示下面的图像。在一个测试应用程序中,我将红色图像的图像视图放在蓝色图像的上方。
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *redImageView;
@property (weak, nonatomic) IBOutlet UIImageView *blueImageView;
@property (strong,nonatomic) UIBezierPath *maskPath;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.maskPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 0, self.redImageView.bounds.size.height)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.redImageView.bounds;
maskLayer.path = self.maskPath.CGPath;
self.redImageView.layer.mask = maskLayer;
[self performSelector:@selector(enlargeMask:) withObject:maskLayer afterDelay:2];
}
-(void)enlargeMask:(CAShapeLayer *) shapeLayer {
UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:self.redImageView.bounds];
CABasicAnimation* pathAnim = [CABasicAnimation animationWithKeyPath: @"path"];
pathAnim.fromValue = (id)self.maskPath.CGPath;
pathAnim.toValue = (id)newPath.CGPath;
pathAnim.duration = 1;
shapeLayer.path = newPath.CGPath;
[shapeLayer addAnimation:pathAnim forKey:@"path"];
}