Skip to content

Commit

Permalink
Merge pull request #15 from andreamazz/fix-background-glitch
Browse files Browse the repository at this point in the history
Fix background glitch
  • Loading branch information
antiguab committed Jul 14, 2015
2 parents ef8823a + 44231e4 commit 23c7f86
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
22 changes: 22 additions & 0 deletions Example/BAFluidView/BAViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ - (void)viewDidLoad {
selector:@selector(showSwipeForNextExampleLabel)
userInfo:nil
repeats:YES];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(stopAnimation)
name:UIApplicationWillResignActiveNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(startAnimation)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}

- (void)stopAnimation {
if ([self.exampleContainerView isKindOfClass:[BAFluidView class]]) {
[(BAFluidView *)self.exampleContainerView stopAnimation];
}
}

- (void)startAnimation {
if ([self.exampleContainerView isKindOfClass:[BAFluidView class]]) {
[(BAFluidView *)self.exampleContainerView startAnimation];
}
}

- (void)viewDidLayoutSubviews {
Expand Down
6 changes: 5 additions & 1 deletion Pod/Classes/BAFluidView.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ This method lets you choose to what level you want the fluidVIew to increase or
*/
- (void)startAnimation;

/**
This methods stops all the desired animations
*/
- (void)stopAnimation;

/**
This method can set all the default values prior to start of animation
*/
- (void)initialize;


@end
62 changes: 39 additions & 23 deletions Pod/Classes/BAFluidView.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @interface BAFluidView()
@property (strong,nonatomic) NSNumber* fillLevel;
@property (assign,nonatomic) BOOL initialFill;

@property (assign,nonatomic) BOOL animating;

@property (assign,nonatomic) int waveLength;//** 2 UIBezierPaths = 1 wavelength
@property (assign,nonatomic) int finalX;
Expand Down Expand Up @@ -212,28 +213,40 @@ - (void)initialize {
}

- (void)startAnimation {
self.startingAmplitude = self.maxAmplitude;

//Phase Shift Animation
CAKeyframeAnimation *horizontalAnimation =
[CAKeyframeAnimation animationWithKeyPath:@"position.x"];
horizontalAnimation.values = @[@(self.lineLayer.position.x),@(-self.finalX + self.waveLength)];
horizontalAnimation.duration = 1.0;
horizontalAnimation.repeatCount = HUGE;
[self.lineLayer addAnimation:horizontalAnimation forKey:@"horizontalAnimation"];

//Wave Crest Animations
self.waveCrestAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
self.waveCrestAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
self.waveCrestAnimation.values = [self getBezierPathValues];
self.waveCrestAnimation.duration = 0.5;
self.waveCrestAnimation.removedOnCompletion = NO;
self.waveCrestAnimation.fillMode = kCAFillModeForwards;
self.waveCrestAnimation.delegate = self;
[self updateWaveSegmentAnimation];

//add sublayer to view
[self.layer addSublayer:self.lineLayer];
if (!self.animating) {
self.startingAmplitude = self.maxAmplitude;

//Phase Shift Animation
CAKeyframeAnimation *horizontalAnimation =
[CAKeyframeAnimation animationWithKeyPath:@"position.x"];
horizontalAnimation.values = @[@(self.lineLayer.position.x),@(-self.finalX + self.waveLength)];
horizontalAnimation.duration = 1.0;
horizontalAnimation.repeatCount = HUGE;
[self.lineLayer addAnimation:horizontalAnimation forKey:@"horizontalAnimation"];

//Wave Crest Animations
self.waveCrestAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
self.waveCrestAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
self.waveCrestAnimation.values = [self getBezierPathValues];
self.waveCrestAnimation.duration = 0.5;
self.waveCrestAnimation.removedOnCompletion = NO;
self.waveCrestAnimation.fillMode = kCAFillModeForwards;
self.waveCrestAnimation.delegate = self;
[self updateWaveSegmentAnimation];

//add sublayer to view
[self.layer addSublayer:self.lineLayer];

self.animating = YES;
}
}

- (void)stopAnimation {
[self.lineLayer removeAnimationForKey:@"horizontalAnimation"];
[self.lineLayer removeAnimationForKey:@"waveSegmentAnimation"];
self.waveCrestAnimation = nil;

self.animating = NO;
}

- (void)keepStationary {
Expand Down Expand Up @@ -284,7 +297,10 @@ - (void)updateWaveSegmentAnimation {
self.waveCrestAnimation.values = [self getBezierPathValues];
[CATransaction setCompletionBlock:^{
//keeps it repeating but also changing in wave size
[self updateWaveSegmentAnimation];
if (self.animating) {
[self updateWaveSegmentAnimation];
}

}];
[self.lineLayer addAnimation:self.waveCrestAnimation forKey:@"waveSegmentAnimation"];
[CATransaction commit];
Expand Down

0 comments on commit 23c7f86

Please sign in to comment.