//、、海浪动画
@property(nonatomic, strong) UIView*back;
@property(nonatomic, strong) CAShapeLayer*waveShapeLayer;
@property(nonatomic, assign) CGFloato ffsetX;
@property(nonatomic, assign) CGFloat waveHeight;
//、、创建波动的UIVIEW
_back = [[UIView alloc] initWithFrame:CGRectMake(0, 130, self.view.frame.size.width, 50)];
[self.view addSubview:_back];
self.waveShapeLayer = [CAShapeLayer layer];
//默认背景为白色 定义颜色
self.waveShapeLayer.fillColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor;
[self.back.layer addSublayer:self.waveShapeLayer];
//、、启动动画
CADisplayLink *displayLink= [CADisplayLink displayLinkWithTarget:selfselector:@selector(getCurrentWave)];
[displayLink addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSRunLoopCommonModes];
- (void)getCurrentWave {
//offsetX决定x位置,如果想搞明白可以多试几次
self.offsetX+=0.01;
//声明第一条波曲线的路径
CGMutablePathRefpath = CGPathCreateMutable();
//设置起始点
CGPathMoveToPoint(path, nil, 0, self.waveHeight);
CGFloat y = 0.f;
//波纹的公式
for(floatx = 0.f; x <= self.waveWidth; x++) {
// y=上下波动幅度* sin(起伏的个数* 个数之间的角度+ 左右偏移(走动效果))+上下偏移度数
y = 30* sin((400/ _back.frame.size.width) * (x * M_PI/ 200) - self.offsetX) + self.waveHeight;
CGPathAddLineToPoint(path, nil, x, y);
}
//把绘图信息添加到路径里
CGPathAddLineToPoint(path, nil, self.waveWidth, _back.frame.size.height);
CGPathAddLineToPoint(path, nil, 0, _A.frame.size.height);
//结束绘图信息
CGPathCloseSubpath(path);
self.waveShapeLayer.path= path;
//释放绘图路径
CGPathRelease(path);
// y=Asin(ωx+φ)+k
// A :振幅,曲线最高位和最低位的距离
//
// ω :角速度,用于控制周期大小,单位x中起伏的个数
//
// K :偏距,曲线上下偏移量
//
// φ :初相,曲线左右偏移量
}