Skip to content

Instantly share code, notes, and snippets.

@parachvte
Created August 5, 2015 06:40
Show Gist options
  • Save parachvte/91edeaef1e770ffa4db0 to your computer and use it in GitHub Desktop.
Save parachvte/91edeaef1e770ffa4db0 to your computer and use it in GitHub Desktop.
iOS - WaveLoadingView
#import <UIKit/UIKit.h>
@interface RXWaveLoadingView : UIView
@property (nonatomic, assign) NSInteger numberOfWaves;
@property (nonatomic, assign) CGSize waveSize;
@property (nonatomic, assign) CGFloat waveSpacing;
@property (nonatomic, assign) CGFloat waveDuration;
@property (nonatomic, strong) UIColor *waveColor;
- (void)startAnimating;
- (void)stopAnimating;
@end
#import "RXWaveLoadingView.h"
@implementation RXWaveLoadingView
static NSString *kRXWaveLoadingView = @"RXWaveLoadingView";
- (instancetype)init {
self = [super init];
if (self) {
_numberOfWaves = 5;
_waveSize = CGSizeMake(10, 40);
_waveSpacing = 3;
_waveColor = [UIColor lightGrayColor];
_waveDuration = 0.1;
}
return self;
}
- (CAAnimation*)addAnimateWithDelay:(CGFloat)delay {
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
animation.repeatCount = MAXFLOAT;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSNumber numberWithFloat:M_PI / _numberOfWaves];
animation.toValue = [NSNumber numberWithFloat:M_PI + M_PI / _numberOfWaves];
animation.duration = _numberOfWaves * _waveDuration;
animation.beginTime = CACurrentMediaTime() + delay;
return animation;
}
- (void)startAnimating {
[self stopAnimating];
for (int i = 0; i < _numberOfWaves; i++) {
CGRect frame = CGRectMake(i * (_waveSize.width + _waveSpacing), 0, _waveSize.width, _waveSize.height);
UIView *waveView = [[UIView alloc] initWithFrame:frame];
[waveView setBackgroundColor:_waveColor];
[waveView.layer addAnimation:[self addAnimateWithDelay:i * _waveDuration] forKey:kRXWaveLoadingView];
[self addSubview:waveView];
}
self.hidden = NO;
}
- (void)stopAnimating {
if (self.subviews.count) {
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
self.hidden = YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment