# 为视图添加丝滑的水波纹

#### 首先我们可以把如此丝滑的水波纹拆分一下下：

• 一条规律的曲线。
• 曲线匀速向右移动。
• 曲线下方的位置用颜色填充。

#### 于是先来一条曲线吧。

f(x) = Asin(ωx+φ)+k

``extern double sin(double);复制代码``

``````CGFloat y = 0.f;
for (CGFloat x = 0.f; x <= CGRectGetWidth(self.frame)="" ;="" x++)="" {="" y="height" *="" sin(self.angularSpeed="" x="" +="" self.offsetX);="" CGPathAddLineToPoint(path,="" NULL,="" x,="" y);="" }<="" code=""/>复制代码``````

#### 让它动起来

``self.waveDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(currentWave)];复制代码``

``self.offsetX -= self.waveSpeed;复制代码``

#### 涂个颜色

``````CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, height / 2);

CGFloat y = 0.f;
for (CGFloat x = 0.f; x <= CGRectGetWidth(self.frame)="" ;="" x++)="" {="" y="height" *="" sin(self.angularSpeed="" x="" +="" self.offsetX);="" CGPathAddLineToPoint(path,="" NULL,="" x,="" y);="" }="" width,="" height);="" 0,="" CGPathCloseSubpath(path);<="" code=""/>复制代码``````

``self.waveShapeLayer.fillColor = self.waveColor.CGColor;复制代码``

#### 最后

Github: WXWaveView