iOS实现波浪效果
程序员文章站
2023-12-17 20:09:34
本文实例为大家分享了ios实现波浪效果的具体代码,供大家参考,具体内容如下
代码:
@interface viewcontroller ()
@prop...
本文实例为大家分享了ios实现波浪效果的具体代码,供大家参考,具体内容如下
代码:
@interface viewcontroller () @property (strong, nonatomic) cadisplaylink *displaylink; @property (strong, nonatomic) cashapelayer *shapelayer; @property (strong, nonatomic) uibezierpath *path; @property (strong, nonatomic) cashapelayer *shapelayer2; @property (strong, nonatomic) uibezierpath *path2; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; _shapelayer = [cashapelayer layer]; _shapelayer.frame = cgrectmake(0, 100, 375, 150); [self.view.layer addsublayer:_shapelayer]; _shapelayer2 = [cashapelayer layer]; _shapelayer2.frame = cgrectmake(0, 100, 375, 150); [self.view.layer addsublayer:_shapelayer2]; _shapelayer.fillcolor = [[uicolor yellowcolor] colorwithalphacomponent:0.3].cgcolor; _shapelayer2.fillcolor = [[uicolor bluecolor] colorwithalphacomponent:0.3].cgcolor; _displaylink = [cadisplaylink displaylinkwithtarget:self selector:@selector(drawpath)]; [_displaylink addtorunloop:[nsrunloop mainrunloop] formode:nsrunloopcommonmodes]; } - (void)drawpath { static double i = 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越界 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。