Skip to content

Commit

Permalink
1. Fixed issue with tint color not being set properly;
Browse files Browse the repository at this point in the history
2. Fixed issue with auto layout.
  • Loading branch information
Danil Gontovnik committed Aug 10, 2016
1 parent c13ec64 commit 35533f5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DGActivityIndicatorView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Pod::Spec.new do |s|

s.name = "DGActivityIndicatorView"
s.version = "2.1"
s.authors = { "Danil Gontovnik" => "gontovnik.danil@gmail.com" }
s.authors = { "Danil Gontovnik" => "danil@gontovnik.com" }
s.homepage = "https://github.com/gontovnik/DGActivityIndicatorView"
s.summary = "DGActivityIndicatorView is a great way to make loading spinners in your application look nicer."
s.source = { :git => "https://github.com/gontovnik/DGActivityIndicatorView.git",
Expand Down
65 changes: 55 additions & 10 deletions DGActivityIndicatorView/DGActivityIndicatorView.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,27 @@

static const CGFloat kDGActivityIndicatorDefaultSize = 40.0f;

@interface DGActivityIndicatorView () {
CALayer *_animationLayer;
}

@end

@implementation DGActivityIndicatorView

#pragma mark -
#pragma mark Constructors

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_tintColor = [UIColor whiteColor];
_size = kDGActivityIndicatorDefaultSize;
[self commonInit];
}
return self;
}

- (id)initWithType:(DGActivityIndicatorAnimationType)type {
return [self initWithType:type tintColor:[UIColor whiteColor] size:kDGActivityIndicatorDefaultSize];
}
Expand All @@ -63,37 +79,44 @@ - (id)initWithType:(DGActivityIndicatorAnimationType)type tintColor:(UIColor *)t
_type = type;
_size = size;
_tintColor = tintColor;
self.userInteractionEnabled = NO;
self.hidden = YES;
[self commonInit];
}
return self;
}

#pragma mark -
#pragma mark Methods

- (void)commonInit {
self.userInteractionEnabled = NO;
self.hidden = YES;

_animationLayer = [[CALayer alloc] init];
[self.layer addSublayer:_animationLayer];
}

- (void)setupAnimation {
self.layer.sublayers = nil;
_animationLayer.sublayers = nil;

id<DGActivityIndicatorAnimationProtocol> animation = [DGActivityIndicatorView activityIndicatorAnimationForAnimationType:_type];

if ([animation respondsToSelector:@selector(setupAnimationInLayer:withSize:tintColor:)]) {
[animation setupAnimationInLayer:self.layer withSize:CGSizeMake(_size, _size) tintColor:_tintColor];
self.layer.speed = 0.0f;
[animation setupAnimationInLayer:_animationLayer withSize:CGSizeMake(_size, _size) tintColor:_tintColor];
_animationLayer.speed = 0.0f;
}
}

- (void)startAnimating {
if (!self.layer.sublayers) {
if (!_animationLayer.sublayers) {
[self setupAnimation];
}
self.hidden = NO;
self.layer.speed = 1.0f;
_animationLayer.speed = 1.0f;
_animating = YES;
}

- (void)stopAnimating {
self.layer.speed = 0.0f;
_animationLayer.speed = 0.0f;
_animating = NO;
self.hidden = YES;
}
Expand Down Expand Up @@ -121,8 +144,15 @@ - (void)setTintColor:(UIColor *)tintColor {
if (![_tintColor isEqual:tintColor]) {
_tintColor = tintColor;

for (CALayer *sublayer in self.layer.sublayers) {
sublayer.backgroundColor = tintColor.CGColor;
CGColorRef tintColorRef = tintColor.CGColor;
for (CALayer *sublayer in _animationLayer.sublayers) {
sublayer.backgroundColor = tintColorRef;

if ([sublayer isKindOfClass:[CAShapeLayer class]]) {
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.strokeColor = tintColorRef;
shapeLayer.fillColor = tintColorRef;
}
}
}
}
Expand Down Expand Up @@ -202,4 +232,19 @@ - (void)setTintColor:(UIColor *)tintColor {
return nil;
}

#pragma mark -
#pragma mark Layout

- (void)layoutSubviews {
[super layoutSubviews];

_animationLayer.frame = self.bounds;

if (_animating) {
[self stopAnimating];
[self setupAnimation];
[self startAnimating];
}
}

@end

0 comments on commit 35533f5

Please sign in to comment.