Permalink
Browse files

Implement UIAppearance

  • Loading branch information...
0xced committed Jul 5, 2012
1 parent 975bb3f commit f5fbf993b432eeedd3d8110f346361b33cf6482f
Showing with 67 additions and 44 deletions.
  1. +4 −4 DACircularProgress/DACircularProgressView.h
  2. +63 −40 DACircularProgress/DACircularProgressView.m
@@ -10,10 +10,10 @@
@interface DACircularProgressView : UIView
@property(nonatomic, strong) UIColor *trackTintColor;
@property(nonatomic, strong) UIColor *progressTintColor;
@property(nonatomic) BOOL roundedCorners;
@property(nonatomic) CGFloat thicknessRatio;
@property(nonatomic, strong) UIColor *trackTintColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong) UIColor *progressTintColor UI_APPEARANCE_SELECTOR;
@property(nonatomic) NSInteger roundedCorners UI_APPEARANCE_SELECTOR; // Can not use BOOL with UI_APPEARANCE_SELECTOR :-(
@property(nonatomic) CGFloat thicknessRatio UI_APPEARANCE_SELECTOR;
@property(nonatomic) CGFloat progress;
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated;
@@ -14,7 +14,7 @@ @interface DACircularProgressLayer : CALayer
@property(nonatomic, strong) UIColor *trackTintColor;
@property(nonatomic, strong) UIColor *progressTintColor;
@property(nonatomic) BOOL roundedCorners;
@property(nonatomic) NSInteger roundedCorners;
@property(nonatomic) CGFloat thicknessRatio;
@property(nonatomic) CGFloat progress;
@@ -77,7 +77,7 @@ - (void)drawInContext:(CGContextRef)context
CGPathRelease(progressPath);
}
if (progress > 0.f && self.roundedCorners == YES)
if (progress > 0.f && self.roundedCorners)
{
CGFloat pathWidth = radius * self.thicknessRatio;
CGFloat xOffset = radius * (1.f + ((1 - (self.thicknessRatio / 2.f)) * cosf(radians)));
@@ -102,29 +102,31 @@ - (void)drawInContext:(CGContextRef)context
@implementation DACircularProgressView
@dynamic trackTintColor;
@dynamic progressTintColor;
@dynamic roundedCorners;
@dynamic thicknessRatio;
@dynamic progress;
+ (void) initialize
{
if (self != [DACircularProgressView class])
return;
id appearance = [self appearance];
[appearance setTrackTintColor:[[UIColor whiteColor] colorWithAlphaComponent:0.3f]];
[appearance setProgressTintColor:[UIColor whiteColor]];
[appearance setThicknessRatio:0.3f];
[appearance setRoundedCorners:NO];
}
+ (Class)layerClass
{
return [DACircularProgressLayer class];
}
- (id)init
- (DACircularProgressLayer *)circularProgressLayer
{
return [self initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)];
return (DACircularProgressLayer *)self.layer;
}
- (void)commonInit
- (id)init
{
self.layer.contentsScale = [UIScreen mainScreen].scale;
self.trackTintColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.3f];
self.progressTintColor = [UIColor whiteColor];
self.thicknessRatio = 0.3f;
self.roundedCorners = NO;
return [self initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)];
}
- (id)initWithFrame:(CGRect)frame
@@ -133,31 +135,29 @@ - (id)initWithFrame:(CGRect)frame
if (self)
{
self.backgroundColor = [UIColor clearColor];
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder
- (void)didMoveToWindow
{
self = [super initWithCoder:decoder];
if (self)
{
[self commonInit];
}
return self;
self.circularProgressLayer.contentsScale = [UIScreen mainScreen].scale;
}
#pragma mark - Progress
-(CGFloat)progress
{
return self.circularProgressLayer.progress;
}
- (void)setProgress:(CGFloat)progress
{
[self setProgress:progress animated:NO];
}
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated
{
DACircularProgressLayer *layer = (DACircularProgressLayer *)self.layer;
CGFloat pinnedProgress = MIN(MAX(progress, 0.f), 1.f);
if (animated)
{
@@ -166,36 +166,59 @@ - (void)setProgress:(CGFloat)progress animated:(BOOL)animated
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = [NSNumber numberWithFloat:self.progress];
animation.toValue = [NSNumber numberWithFloat:pinnedProgress];
[layer addAnimation:animation forKey:@"progress"];
[self.circularProgressLayer addAnimation:animation forKey:@"progress"];
}
else
{
[layer setNeedsDisplay];
[self.circularProgressLayer setNeedsDisplay];
}
layer.progress = pinnedProgress;
self.circularProgressLayer.progress = pinnedProgress;
}
- (void)setThicknessRatio:(CGFloat)thicknessRatio
#pragma mark - UIAppearance methods
- (UIColor *)trackTintColor
{
DACircularProgressLayer *layer = (DACircularProgressLayer *)self.layer;
layer.thicknessRatio = MIN(MAX(thicknessRatio, 0.f), 1.f);
return self.circularProgressLayer.trackTintColor;
}
#pragma mark - Dynamic Properties
- (void)setTrackTintColor:(UIColor *)trackTintColor
{
self.circularProgressLayer.trackTintColor = trackTintColor;
[self.circularProgressLayer setNeedsDisplay];
}
- (id)forwardingTargetForSelector:(SEL)selector
- (UIColor *)progressTintColor
{
if ([NSStringFromSelector(selector) hasPrefix:@"set"])
{
[self.layer setNeedsDisplay];
}
return self.layer;
return self.circularProgressLayer.progressTintColor;
}
- (void)setProgressTintColor:(UIColor *)progressTintColor
{
self.circularProgressLayer.progressTintColor = progressTintColor;
[self.circularProgressLayer setNeedsDisplay];
}
- (NSInteger)roundedCorners
{
return self.roundedCorners;
}
-(void)setRoundedCorners:(NSInteger)roundedCorners
{
self.circularProgressLayer.roundedCorners = roundedCorners;
[self.circularProgressLayer setNeedsDisplay];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
-(CGFloat)thicknessRatio
{
return self.circularProgressLayer.thicknessRatio;
}
- (void)setThicknessRatio:(CGFloat)thicknessRatio
{
[self.layer setNeedsDisplay];
[self.layer setValue:value forKey:key];
self.circularProgressLayer.thicknessRatio = MIN(MAX(thicknessRatio, 0.f), 1.f);
[self.circularProgressLayer setNeedsDisplay];
}
@end

0 comments on commit f5fbf99

Please sign in to comment.