iOS- 波浪效果简单实现

//只跟相位有关

- (void)creat4{

CGFloatW =CGRectGetWidth(self.view.bounds);

CGFloatH =CGRectGetHeight(self.view.bounds);

//公式:ω = 2π/T

//设置波的宽度是容器的宽度,希望能展示0.5个波曲线,周期为T=W/0.5;

//ω = 2π/T -> π/W

CGFloatw =M_PI/W;

if(self.shapeLayer==nil){

CAShapeLayer* layer = [CAShapeLayerlayer];

layer.fillColor= [UIColorcolorWithRed:211/255.0green:10/255.0blue:15/255.0alpha:1].CGColor;

[self.view.layeraddSublayer:layer];

self.shapeLayer= layer;

}

//振幅

self.waveA=20;

//ω常量

self.waveW= w;

//y轴偏移

self.currentK= H/2;

//相位

self.waveSpeed=0.05;

[selfdisplayLinkStar];

}

#pragma mark- CADisplayLink计时器

//每一帧刷新一次,比NSTimer准确

- (void)displayLinkStar{

CADisplayLink* link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(waveAnimation)];

[linkaddToRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSRunLoopCommonModes];

}

#pragma mark-绘制波浪

- (void)waveAnimation{

self.offsetX+=self.waveSpeed;

CGFloatW =CGRectGetWidth(self.view.bounds);

CGFloatH =CGRectGetHeight(self.view.bounds);

CGMutablePathRefpath =CGPathCreateMutable();

CGFloaty =self.currentK;

CGPathMoveToPoint(path,nil,0, y);

for(NSIntegerx =0; x <= W; x++) {

y =self.waveA*sinf(self.waveW*x +self.offsetX) +_currentK;

CGPathAddLineToPoint(path,nil, x, y);

}

CGPathAddLineToPoint(path,nil,W, H*2/3);

CGPathAddLineToPoint(path,nil,0, H*2/3);

//self.shapeLayer.fillColor = [UIColor redColor].CGColor;

CGPathCloseSubpath(path);

self.shapeLayer.path= path;

CGPathRelease(path);

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容