动画图像擦除波形颜色变化

时间:2014-07-09 02:09:59

标签: ios objective-c cocoa-touch uiview uiimage

我有一个UIImage的波形是根据音频文件的样本创建的。波形为蓝色,背景为白色。我希望蓝色波形在播放音频时变为红色以指示进度。如果我有两个相同波形的UIImage:一个是蓝色的,一个是红色的 - 是否有某种动画图像擦拭我可以做到这一点?

当波形为UIView并且我只是在蓝色UIView波形上绘制红色UIView波形时,我能够实现这一点,但是如果我有多个波形(我这样做 - 在tableview),这使我的CPU使用率超过95%并导致很多延迟 - 即使没有播放音频。我相信如果我走UIImage路线,因为我只有静态图像,所以事情会更顺畅。

任何提示?

1 个答案:

答案 0 :(得分:1)

据我所知,唯一的方法是将两个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"];

}
相关问题