-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDoubleWaves.m
252 lines (185 loc) · 6.58 KB
/
DoubleWaves.m
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//
// DoubleWaves.m
// DoubleWaves
//
// Created by cAibDe on 2016/12/21.
// Copyright © 2016年 cAibDe. All rights reserved.
//
#import "DoubleWaves.h"
@interface DoubleWaves ()
@property (nonatomic,strong)CADisplayLink *firstWaveDisplayLink;
@property (nonatomic,strong)CAShapeLayer *firstWavesLayer;
@property (nonatomic,strong)UIColor *firstWavesColor;
@property (nonatomic,strong)CADisplayLink *secondWaveDisplayLink;
@property (nonatomic,strong)CAShapeLayer *secondWavesLayer;
@property (nonatomic,strong)UIColor *secondWavesColor;
@property (nonatomic,strong)UIView *sinView;
@property (nonatomic,strong)UIView *cosView;
@end
@implementation DoubleWaves
{
CGFloat waveA;//水纹振幅
CGFloat waveW ;//水纹周期
CGFloat offsetX1; //位移
CGFloat currentK; //当前波浪高度Y
CGFloat offsetX2; //位移
CGFloat wavesSpeed;//水纹速度
CGFloat WavesWidth; //水纹宽度
}
- (UIView *)sinView{
if (!_sinView) {
_sinView = [[UIView alloc]initWithFrame:self.bounds];
_sinView.alpha = 0.6;
}
return _sinView;
}
- (UIView *)cosView{
if (!_cosView) {
_cosView = [[UIView alloc]initWithFrame:self.bounds];
_cosView.alpha = 0.6;
}
return _cosView;
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.sinView];
[self addSubview:self.cosView];
[self setUpFirstWave];
[self setUpSecondWave];
}
return self;
}
- (void)setUpSecondWave{
//设置波浪的宽度
WavesWidth = self.frame.size.width;
//第二个波浪颜色
self.secondWavesColor = [UIColor colorWithRed:86/255.0f green:202/255.0f blue:139/255.0f alpha:1];
//设置波浪的速度
wavesSpeed = 1/M_PI;
//初始化layer
if (self.secondWavesLayer == nil) {
//初始化
self.secondWavesLayer = [CAShapeLayer layer];
//设置闭环的颜色
self.secondWavesLayer.fillColor = self.secondWavesColor.CGColor;
//设置边缘线的颜色
//_firstWaveLayer.strokeColor = [UIColor blueColor].CGColor;
//设置边缘线的宽度
//self.firstWavesLayer.lineWidth = 1.0;
// self.firstWavesLayer.strokeStart = 0.0;
// self.firstWavesLayer.strokeEnd = 0.8;
[self.cosView.layer addSublayer:self.secondWavesLayer];
}
//同正弦函数相同,无交错效果
wavesSpeed = 0.02;
//设置振幅
waveA = 12;
//设置周期
waveW = 0.5/30.0;
//同正弦函数不同,会有交错效果
//设置波浪流动速度
// wavesSpeed = 0.04;
// //设置振幅
// waveA = 13;
// //设置周期
// waveW = 0.5/30.0;
//设置波浪纵向位置
currentK = self.frame.size.height/2;//屏幕居中
//启动定时器
self.secondWaveDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getSecondWave:)];
[self.secondWaveDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
-(void)getSecondWave:(CADisplayLink *)displayLink{
//实时的位移
//实时的位移
offsetX2 += wavesSpeed;
[self setSecondWaveLayerPath];
}
-(void)setSecondWaveLayerPath{
//创建一个路径
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = currentK;
//将点移动到 x=0,y=currentK的位置
CGPathMoveToPoint(path, nil, 0, y);
for (NSInteger i =0.0f; i<=WavesWidth; i++) {
//余弦函数波浪公式
y = waveA * cos(waveW*i + offsetX2)+currentK;
//如果需要正弦函数的峰顶和余弦函数的峰底对应,可以替换成下方公式均可
//y = waveA * cos(waveW*i + offsetX+M_PI_2)+currentK;
//y = waveA * sin(-(waveW*i + offsetX))+currentK;
//将点连成线
CGPathAddLineToPoint(path, nil, i, y);
}
CGPathAddLineToPoint(path, nil, WavesWidth, 0);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);
self.secondWavesLayer.path = path;
//使用layer 而没用CurrentContext
CGPathRelease(path);
}
- (void)setUpFirstWave{
//设置波浪的宽度
WavesWidth = self.frame.size.width;
//第一个波浪颜色
self.firstWavesColor = [UIColor colorWithRed:86/255.0f green:202/255.0f blue:139/255.0f alpha:1];
//设置波浪的速度
wavesSpeed = 1/M_PI;
//初始化layer
if (self.firstWavesLayer == nil) {
//初始化
self.firstWavesLayer = [CAShapeLayer layer];
//设置闭环的颜色
self.firstWavesLayer.fillColor = self.firstWavesColor.CGColor;
//设置边缘线的颜色
//_firstWaveLayer.strokeColor = [UIColor blueColor].CGColor;
//设置边缘线的宽度
//self.firstWavesLayer.lineWidth = 1.0;
// self.firstWavesLayer.strokeStart = 0.0;
// self.firstWavesLayer.strokeEnd = 0.8;
[self.sinView.layer addSublayer:self.firstWavesLayer];
}
//设置波浪流动速度
wavesSpeed = 0.02;
//设置振幅
waveA = 12;
//设置周期
waveW = 0.5/30.0;
//设置波浪纵向位置
currentK = self.frame.size.height/2;//屏幕居中
//启动定时器
self.firstWaveDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getFirstWave:)];
[self.firstWaveDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
-(void)getFirstWave:(CADisplayLink *)displayLink{
//实时的位移
//实时的位移
offsetX1 += wavesSpeed;
[self setFirstWaveLayerPath];
}
-(void)setFirstWaveLayerPath{
//创建一个路径
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = currentK;
//将点移动到 x=0,y=currentK的位置
CGPathMoveToPoint(path, nil, 0, y);
for (NSInteger i =0.0f; i<=WavesWidth; i++) {
//正弦函数波浪公式
y = waveA * sin(waveW * i+ offsetX1)+currentK;
//将点连成线
CGPathAddLineToPoint(path, nil, i, y);
}
CGPathAddLineToPoint(path, nil, WavesWidth, 0);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);
self.firstWavesLayer.path = path;
//使用layer 而没用CurrentContext
CGPathRelease(path);
}
-(void)dealloc
{
[self.firstWaveDisplayLink invalidate];
[self.secondWaveDisplayLink invalidate];
}
@end