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.waveAsinf(self.waveWx +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);

}

你可能感兴趣的:(iOS- 波浪效果简单实现)