您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家分享的是有关iOS如何实现波浪效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
本文实例为大家分享了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 *path3; @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]; _path3 = [UIBezierPath bezierPath]; [_path moveToPoint:CGPointZero]; [_path3 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)]; [_path3 addLineToPoint:CGPointMake(x, y2)]; } [_path addLineToPoint:CGPointMake(375, -100)]; [_path addLineToPoint:CGPointMake(0, -100)]; _path.lineWidth = 1; _shapeLayer.path = _path.CGPath; [_path3 addLineToPoint:CGPointMake(375, -100)]; [_path3 addLineToPoint:CGPointMake(0, -100)]; _path3.lineWidth = 1; _shapeLayer2.path = _path3.CGPath; i += 0.1; if (i > M_PI * 2) { i = 0;//防止i越界 } }
感谢各位的阅读!关于“iOS如何实现波浪效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。