代码库> 视图动画> 自定义带有渐变色的波浪动画
自定义带有渐变色的波浪动画
关注
查看次数:4284 下载次数:841
上传时间:2017-08-22 大小:59 B
1、波纹动画主要依赖于CAShapeLayer的绘制,使用帧动画实现;需要使用多个CAShapeLayer通过y值变换组成(这里我只是用了2个CAShapeLayer)。
2、渐变色由CAGradientLayer完成。
```
- (void)changeFirstWaveLayerPath {
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = _wavePointY;

CGPathMoveToPoint(path, nil, 0, y);
for (float x = 0.0f; x <= _waveWidth; x ++) {
y = _waveAmplitude * 1.6 * sin((250 / _waveWidth) * (x * M_PI / 180) - _waveOffsetX * M_PI / 270) + _wavePointY;
CGPathAddLineToPoint(path, nil, x, y);
}

CGPathAddLineToPoint(path, nil, _waveWidth, 0);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);

_shapeLayer1.path = path;
CGPathRelease(path);
}

- (void)changeSecondWaveLayerPath {
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = _wavePointY;
CGPathMoveToPoint(path, nil, 0, y);
for (float x = 0.0f; x <= _waveWidth; x ++) {

y = _waveAmplitude * 1.6 * sin((250 / _waveWidth) * (x * M_PI / 180) - _waveOffsetX * M_PI / 180) + _wavePointY;
CGPathAddLineToPoint(path, nil, x, y);
}

CGPathAddLineToPoint(path, nil, _waveWidth, 0);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);

_shapeLayer2.path = path;

CGPathRelease(path);
}
```

方法调用:
```
_waveOffsetX += _waveSpeed;
[self changeFirstWaveLayerPath];
[self changeSecondWaveLayerPath];

[self.layer addSublayer:self.gradientLayer1];
self.gradientLayer1.mask = _shapeLayer1;

[self.layer addSublayer:self.gradientLayer2];
self.gradientLayer2.mask = _shapeLayer2;
```
收藏
我来说两句
发表评论
您还没有登录!请登录注册
所有评论(0
提示
sina weixin mail 回到顶部