## 需要预先了解的概念（Understanding）

``````+ (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel;

``@property(readonly, nonatomic) CFTimeInterval duration;复制代码``

duration只读，每帧之间的时间，每次刷新之间的时间间隔。可以通过这个时间计算下一帧要显示的的UI的数值。需要注意的是 duration 只是大概的时间。如果cpu忙于其他计算，会跳过几次回调。

``@property(nonatomic) NSInteger frameInterval;复制代码``

frameInterval可读写，隔多少帧调用一次selector，默认1，意即每帧调用一次，iOS刷新频率60HZ，意即每秒调用60次。

### 正弦波浪公式

y=Asin(ωx+φ)+k

• A——振幅，当物体作轨迹符合正弦曲线的直线往复运动时，其值为行程的1/2
• ω——角速度， 控制正弦周期(单位角度内震动的次数)
• φ——初相，x=0时的相位；反映在坐标系上则为图像的左右移动
• k——偏距，反映在坐标系上则为图像的上移或下移

### CAShapeLayer

``@property(nullable) CGPathRef path;复制代码``

## 联合使用

``````UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
view.backgroundColor = [UIColor whiteColor];
CAShapeLayer *firstWaveLayer = [CAShapeLayer layer];
firstWaveLayer.fillColor = [UIColor lightGrayColor].CGColor;
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = 50;
CGPathMoveToPoint(path, nil, 0, y);
CGFloat waveWidth = self.view.frame.size.width;
CGFloat cycle = 6 * M_PI / self.view.frame.size.width;
CGFloat offsetX = 0;
for (float x = 0.0f; x 复制代码``````

``````CAShapeLayer *secondWaveLayer = [CAShapeLayer layer];
secondWaveLayer.fillColor = [UIColor redColor].CGColor;
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = 50;
CGPathMoveToPoint(path, nil, 0, y);
for (float x = 0.0f; x 复制代码``````

``CGFloat offsetX = M_PI/cycle/2;  // also equal 2*M_PI/_cycle/4;复制代码``

``````displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTric)];

``````- (void)displayLinkTric {

static CGFloat offsetX = 0;
offsetX += 0.07;

CGFloat waveWidth = self.view.frame.size.width;
CGFloat cycle = 6 * M_PI / self.view.frame.size.width;

{
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = 50;
CGPathMoveToPoint(path, nil, 0, y);

for (float x = 0.0f; x 复制代码``````

``````

static CGFloat offsetX = 0;
offsetX += 0.05;

static CGFloat amplitude = 8;
static BOOL increase = YES;

if (increase) {
amplitude += 0.04;
} else {
amplitude -= 0.04;
}

if (amplitude >= 12) {
increase = NO;
}
if (amplitude 复制代码``````

``````- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

CGFloat offset = (-scrollView.contentOffset.y-scrollView.contentInset.top);
CGFloat times = offset/10 + 1;
}复制代码``````