iOS 动画家族iOS企业级开发实用技术iOS程序猿

波浪效果的实现

2017-01-11  本文已影响111人  yue博客
y = sin(x) y = 10sin(x) y = 10sin(0.03x) y = 10sin(0.03x + 0.5) y = 10sin(0.03x + 0.5) + 10 y = 10cos(0.03x + 0.5) + 10 双层波 灰色cos 黑色sin

1.创建layer

_shapeLayer= [CAShapeLayer layer];

_shapeLayer.frame=CGRectMake(0,100,375,150);

[self.view.layeraddSublayer:_shapeLayer];

_shapeLayer2= [CAShapeLayer layer];

_shapeLayer2.frame = CGRectMake(0,100,375,150);

[self.view.layer addSublayer:_shapeLayer2];

2.创建定时器

_displayLink = [CADisplayLink displayLinkWithTarget:selfselector:@selector(drawPath)];

[_displayLink addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSRunLoopCommonModes];

3.drawPath方法

static doublei =0;

CGFloat A =10.f;//A振幅

CGFloat k =0;//y轴偏移

CGFloat ω = 0.03;//角速度ω变大,则波形在X轴上收缩(波形变紧密);角速度ω变小,则波形在X轴上延展(波形变稀疏)。不等于0

CGFloat φ = 0 + i;//初相,x=0时的相位;反映在坐标系上则为图像的左右移动。

//y=Asin(ωx+φ)+k

_path= [UIBezierPath bezierPath];

_path2= [UIBezierPath bezierPath];

[_path moveToPoint:CGPointZero];

[_path2 moveToPoint:CGPointZero];

for(int i =0; i < 376; i ++) {

CGFloat x = i;

CGFloat y = A *sin(ω*x+φ)+k;

CGFloat y2 = A *cos(ω*x+φ)+k;

[_path addLineToPoint:CGPointMake(x, y)];

[_path2 addLineToPoint:CGPointMake(x, y2)];

}

[_path addLineToPoint:CGPointMake(375, -100)];

[_path addLineToPoint:CGPointMake(0, -100)];

_path.lineWidth = 1;

_shapeLayer.path = _path.CGPath;

[_path2 addLineToPoint:CGPointMake(375, -100)];

[_path2 addLineToPoint:CGPointMake(0, -100)];

_path2.lineWidth = 1;

_shapeLayer2.path = _path2.CGPath;

i +=0.1;

if(i >M_PI*2) {

i =0;//防止i越界

}

4.效果图

效果图

玩一个有趣的效果,渐变色:

渐变色

demo已更新代码,这里就不贴代码了

demo

上一篇下一篇

猜你喜欢

热点阅读