iOS- 波浪效果簡(jiǎn)單實(shí)現(xiàn)

//只跟相位有關(guān)

- (void)creat4{

CGFloatW =CGRectGetWidth(self.view.bounds);

CGFloatH =CGRectGetHeight(self.view.bounds);

//公式:ω = 2π/T

//設(shè)置波的寬度是容器的寬度,希望能展示0.5個(gè)波曲線,周期為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計(jì)時(shí)器

//每一幀刷新一次,比NSTimer準(zhǔn)確

- (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);

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容