iOS实现波浪效果

栏目: IOS · 发布时间: 7年前

内容简介:本文实例为大家分享了iOS实现波浪效果的具体代码,供大家参考,具体内容如下代码:

本文实例为大家分享了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 *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越界

 }

}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Lean Startup

The Lean Startup

Eric Ries / Crown Business / 2011-9-13 / USD 26.00

更多中文介绍:http://huing.com Most startups fail. But many of those failures are preventable. The Lean Startup is a new approach being adopted across the globe, chan ging the way companies are built and ......一起来看看 《The Lean Startup》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具