Skip to content

Commit 9d40a18

Browse files
committed
add wave start shift
1 parent 7dd9c3d commit 9d40a18

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Example/JWWaveView/TestExamples/MultiCycles/JWMultiCylceWaveTestViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ - (JWWaveView *)waveView {
6868
[UIColor colorWithRed:0 green:0 blue:0.5 alpha:1.0];
6969
_waveView.waveCycles = 2;
7070
_waveView.waveDuration = 2;
71+
_waveView.waveShift = 1.5 * M_PI;
7172
}
7273
return _waveView;
7374
}

JWWaveView/Classes/JWWaveView.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@
4949
*/
5050
@property (nonatomic, assign) CGFloat waveDuration;
5151

52+
/**
53+
The wave path is modeled using the mathmatical sin(x) function. The default value
54+
for x starts at 0, which yeilds an amplitude of 0.
55+
This propery adds a shift to the x value.
56+
57+
The effective range for this property is [0, 2 * M_PI]. The default value is 0,
58+
which means no shift is added to the wave.
59+
60+
@note changing this value after animation has no effect.
61+
*/
62+
@property (nonatomic, assign) CGFloat waveShift;
63+
5264
/**
5365
Start the water waving animation if all conditions are met.
5466
*/

JWWaveView/Classes/JWWaveView.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ @implementation JWWaveView
1919

2020
#pragma mark - Public
2121
- (void)startWavingIfNeeded {
22-
CAReplicatorLayer *replicatorLayer = [self replicatorLayer];
23-
CGRect replicatorRect = replicatorLayer.bounds;
24-
self.waveShapeLayer.frame = replicatorRect;
25-
22+
//force layout sublayers if layouts are pending
23+
[self layoutIfNeeded];
24+
2625
//setup shape
2726
[self renewWavePathWithForceRenew:NO animated:NO];
2827

@@ -65,6 +64,7 @@ - (void)configureIntialSetup {
6564
_waveDuration = 1.0;
6665
_waveColor = [UIColor blueColor];
6766
_waveCycles = 1;
67+
_waveShift = 0;
6868
}
6969

7070
#pragma mark - Override
@@ -165,7 +165,7 @@ - (void)renewWavePathWithForceRenew:(BOOL)forceRenew animated:(BOOL)animated {
165165
for (CGFloat xPos = 0.f; xPos <= canvasWidth; xPos += 1.f) {
166166
//wave path starts at the canvasMidY and uses (canvasHeight * 0.5) as amplitude
167167
CGFloat halfAmplitude = canvasHeight / 4.f;
168-
yPos = canvasMidY + sin((xPos)/canvasWidth * M_PI * 2 * _waveCycles) * halfAmplitude;
168+
yPos = canvasMidY + sin((xPos/canvasWidth) * M_PI * 2 * _waveCycles + _waveShift) * halfAmplitude;
169169
if (fpclassify(xPos) == FP_ZERO) {
170170
[sinPath moveToPoint:(CGPoint){xPos, yPos}];
171171
}
@@ -209,6 +209,13 @@ - (void)setWaveDuration:(CGFloat)waveDuration {
209209
}
210210
}
211211

212+
- (void)setWaveShift:(CGFloat)waveShift {
213+
if (waveShift < 0 || waveShift > 2 * M_PI) {
214+
return;
215+
}
216+
_waveShift = waveShift;
217+
}
218+
212219
#pragma mark - Lazy Loading
213220
- (CAShapeLayer *)waveShapeLayer {
214221
if (!_waveShapeLayer) {

0 commit comments

Comments
 (0)