[ios] Added custom coloring to circular progress.

This commit is contained in:
Ilya Grechuhin 2016-03-04 15:42:12 +03:00 committed by Sergey Yershov
parent ce468566c5
commit 028b9d01b2
4 changed files with 60 additions and 16 deletions

View file

@ -1,3 +1,4 @@
#import "MWMButton.h"
typedef NS_ENUM(NSInteger, MWMCircularProgressState)
{
@ -19,12 +20,15 @@ typedef NS_ENUM(NSInteger, MWMCircularProgressState)
@interface MWMCircularProgress : NSObject
+ (nonnull instancetype)downloaderProgressForParentView:(nonnull UIView *)parentView;
@property (nonatomic) CGFloat progress;
@property (nonatomic) MWMCircularProgressState state;
@property (weak, nonatomic) id<MWMCircularProgressProtocol> delegate;
- (void)setImage:(nonnull UIImage *)image forState:(MWMCircularProgressState)state;
- (void)setColor:(nonnull UIColor *)color forState:(MWMCircularProgressState)state;
- (void)setColoring:(MWMButtonColoring)coloring forState:(MWMCircularProgressState)state;
- (void)setInvertColor:(BOOL)invertColor;
- (nonnull instancetype)init __attribute__((unavailable("init is not available")));

View file

@ -10,6 +10,30 @@
@implementation MWMCircularProgress
+ (nonnull instancetype)downloaderProgressForParentView:(nonnull UIView *)parentView
{
MWMCircularProgress * progress = [[MWMCircularProgress alloc] initWithParentView:parentView];
UIImage * downloadImage = [UIImage imageNamed:@"ic_download"];
[progress setImage:downloadImage forState:MWMCircularProgressStateNormal];
[progress setImage:downloadImage forState:MWMCircularProgressStateSelected];
UIImage * spinnerImage = [UIImage imageNamed:@"ic_close_spinner"];
[progress setImage:spinnerImage forState:MWMCircularProgressStateProgress];
[progress setImage:spinnerImage forState:MWMCircularProgressStateSpinner];
[progress setImage:[UIImage imageNamed:@"ic_download_error"] forState:MWMCircularProgressStateFailed];
[progress setImage:[UIImage imageNamed:@"ic_check"] forState:MWMCircularProgressStateCompleted];
[progress setColoring:MWMButtonColoringBlack forState:MWMCircularProgressStateNormal];
[progress setColoring:MWMButtonColoringBlack forState:MWMCircularProgressStateSelected];
[progress setColoring:MWMButtonColoringBlack forState:MWMCircularProgressStateProgress];
[progress setColoring:MWMButtonColoringBlack forState:MWMCircularProgressStateSpinner];
[progress setColoring:MWMButtonColoringOther forState:MWMCircularProgressStateFailed];
[progress setColoring:MWMButtonColoringBlue forState:MWMCircularProgressStateCompleted];
return progress;
}
- (nonnull instancetype)initWithParentView:(nonnull UIView *)parentView
{
self = [super init];
@ -45,6 +69,11 @@
[self.rootView setColor:color forState:state];
}
- (void)setColoring:(MWMButtonColoring)coloring forState:(MWMCircularProgressState)state
{
[self.rootView setColoring:coloring forState:state];
}
- (void)setInvertColor:(BOOL)invertColor
{
self.rootView.isInvertColor = invertColor;

View file

@ -1,3 +1,5 @@
#import "MWMButton.h"
@interface MWMCircularProgressView : UIView
@property (nonatomic, readonly) BOOL animating;
@ -10,6 +12,7 @@
- (void)setImage:(nonnull UIImage *)image forState:(MWMCircularProgressState)state;
- (void)setColor:(nonnull UIColor *)color forState:(MWMCircularProgressState)state;
- (void)setColoring:(MWMButtonColoring)coloring forState:(MWMCircularProgressState)state;
- (void)animateFromValue:(CGFloat)fromValue toValue:(CGFloat)toValue;

View file

@ -1,10 +1,11 @@
#import "Common.h"
#import "MWMButton.h"
#import "MWMCircularProgress.h"
#import "MWMCircularProgressView.h"
#import "UIColor+MapsMeColor.h"
#import "UIImageView+Coloring.h"
#include "std/map.hpp"
static CGFloat const kLineWidth = 2.0;
static NSString * const kAnimationKey = @"CircleAnimation";
@ -31,11 +32,15 @@ static inline CGFloat angleWithProgress(CGFloat progress)
@end
@implementation MWMCircularProgressView
{
map<MWMCircularProgressState, MWMButtonColoring> m_buttonColoring;
}
- (void)awakeFromNib
{
self.images = [NSMutableDictionary dictionary];
[self setupColors];
[self setupButtonColoring];
[self setupAnimationLayers];
}
@ -61,6 +66,16 @@ static inline CGFloat angleWithProgress(CGFloat progress)
[self setColor:clearColor forState:MWMCircularProgressStateCompleted];
}
- (void)setupButtonColoring
{
[self setColoring:MWMButtonColoringBlack forState:MWMCircularProgressStateNormal];
[self setColoring:MWMButtonColoringBlue forState:MWMCircularProgressStateSelected];
[self setColoring:MWMButtonColoringBlue forState:MWMCircularProgressStateProgress];
[self setColoring:MWMButtonColoringBlue forState:MWMCircularProgressStateSpinner];
[self setColoring:MWMButtonColoringBlue forState:MWMCircularProgressStateFailed];
[self setColoring:MWMButtonColoringBlue forState:MWMCircularProgressStateCompleted];
}
- (void)mwm_refreshUI
{
[self setupColors];
@ -88,6 +103,12 @@ static inline CGFloat angleWithProgress(CGFloat progress)
[self refreshProgress];
}
- (void)setColoring:(MWMButtonColoring)coloring forState:(MWMCircularProgressState)state
{
m_buttonColoring[state] = coloring;
[self refreshProgress];
}
#pragma mark - Progress
- (void)refreshProgress
@ -98,21 +119,8 @@ static inline CGFloat angleWithProgress(CGFloat progress)
self.progressLayer.strokeColor = self.progressLayerColor;
CGRect rect = CGRectInset(self.bounds, kLineWidth, kLineWidth);
self.backgroundLayer.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;
switch (self.state)
{
case MWMCircularProgressStateNormal:
self.button.coloring = MWMButtonColoringBlack;
break;
case MWMCircularProgressStateSelected:
case MWMCircularProgressStateProgress:
case MWMCircularProgressStateSpinner:
case MWMCircularProgressStateFailed:
case MWMCircularProgressStateCompleted:
self.button.coloring = MWMButtonColoringBlue;
break;
}
UIImage * normalImage = self.images[@(self.state)];
[self.button setImage:normalImage forState:UIControlStateNormal];
self.button.coloring = m_buttonColoring[self.state];
[self.button setImage:self.images[@(self.state)] forState:UIControlStateNormal];
}
- (void)updatePath:(CGFloat)progress