iOS实现波浪效果

时间:2022-12-07 22:49:19

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

iOS实现波浪效果

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@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越界
 
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/jukaiit/archive/2018/05/16/9046802.html