forked from n8chur/WNProgressView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WNProgressView.m
178 lines (143 loc) · 5.4 KB
/
WNProgressView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// WNProgressView.m
// BarberPole
//
// Created by Westin Newell on 3/21/12.
// Copyright (c) 2012 n8chur. All rights reserved.
//
#import "WNProgressView.h"
#import <QuartzCore/QuartzCore.h>
@interface WNProgressView ()
- (void)startBarberPole;
- (void)stopBarberPole;
@property (nonatomic) CGFloat progressViewInnerHeight;
@property (nonatomic, strong) UIView* barberPoleView;
@property (nonatomic, strong) CAReplicatorLayer* replicatorLayer;
@end
@implementation WNProgressView
@synthesize barberPoleStripWidth = _barberPoleStripWidth;
@synthesize progressViewInnerHeight = _progressViewInnerHeight;
@synthesize barberPoleView = _barberPoleView;
@synthesize replicatorLayer = _replicatorLayer;
- (void)setupWithFrame:(CGRect)frame
{
[self.barberPoleView removeFromSuperview];
self.barberPoleView = [[UIView alloc] init];
self.barberPoleView.autoresizesSubviews = YES;
UIColor* barColor = nil;
if ( self.progressViewStyle == UIProgressViewStyleBar ) {
self.progressViewInnerHeight = 11;
self.barberPoleView.frame = CGRectMake(0.25f, frame.size.height/2 - self.progressViewInnerHeight/2 + 0.25f, frame.size.width - 0.25f * 4, self.progressViewInnerHeight - 2.5f);
barColor = [UIColor whiteColor];
}
else {
self.progressViewInnerHeight = 9;
self.barberPoleView.frame = CGRectMake(0, frame.size.height/2 - self.progressViewInnerHeight/2, frame.size.width, self.progressViewInnerHeight);
barColor = [UIColor colorWithRed:30.0f/256.0f green:104.0f/256.0f blue:209.0f/256.0f alpha:1];
}
if ( [self respondsToSelector:@selector(progressTintColor)] ) {
if ( self.progressTintColor ) {
barColor = self.progressTintColor;
}
}
CALayer* barberPoleLayer = [CALayer layer];
barberPoleLayer.frame = self.barberPoleView.frame;
CALayer* barberPoleMaskLayer = [CALayer layer];
barberPoleMaskLayer.frame = self.barberPoleView.frame;
barberPoleMaskLayer.cornerRadius = self.progressViewInnerHeight / 2;
// mask doesnt work without a solid background
barberPoleMaskLayer.backgroundColor = [UIColor whiteColor].CGColor;
barberPoleLayer.mask = barberPoleMaskLayer;
CALayer* barberStrip = [CALayer layer];
barberStrip.frame = CGRectMake(0,0,self.barberPoleStripWidth * 2,frame.size.height);
CGMutablePathRef stripPath = CGPathCreateMutable();
CGPathMoveToPoint(stripPath, nil, 0, 0);
CGPathAddLineToPoint(stripPath, nil, self.barberPoleStripWidth, 0);
CGPathAddLineToPoint(stripPath, nil, self.barberPoleStripWidth * 2, barberStrip.frame.size.height);
CGPathAddLineToPoint(stripPath, nil, self.barberPoleStripWidth, barberStrip.frame.size.height);
CAShapeLayer* stripShape = [CAShapeLayer layer];
stripShape.fillColor = barColor.CGColor;
stripShape.path = stripPath;
[barberStrip addSublayer:stripShape];
self.replicatorLayer= [CAReplicatorLayer layer];
self.replicatorLayer.bounds = barberPoleLayer.bounds;
self.replicatorLayer.position = CGPointMake(- barberStrip.frame.size.width * 4, barberPoleLayer.frame.size.height / 2);
self.replicatorLayer.instanceCount = (NSInteger)roundf(frame.size.width / barberStrip.frame.size.width * 2) + 1;
CATransform3D finalTransform = CATransform3DMakeTranslation(barberStrip.frame.size.width, 0, 0);
[self.replicatorLayer setInstanceTransform:finalTransform];
[self.replicatorLayer addSublayer:barberStrip];
[barberPoleLayer addSublayer:self.replicatorLayer];
[self.barberPoleView.layer addSublayer:barberPoleLayer];
self.barberPoleView.alpha = 0.65f;
[self addSubview:self.barberPoleView];
if ( self.progress != 0 ) {
[self stopBarberPole];
}
else {
[self startBarberPole];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self setupWithFrame:self.frame];
}
- (void)setupDefaultValues
{
self.barberPoleStripWidth = self.frame.size.height * 1.5f;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupDefaultValues];
[self setupWithFrame:frame];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setupDefaultValues];
[self setupWithFrame:self.frame];
}
return self;
}
- (void)setProgress:(float)progress
{
[super setProgress:progress];
if ( progress == 0 ) {
if ( self.barberPoleView.hidden == YES) {
[self startBarberPole];
}
}
else {
if ( self.barberPoleView.hidden == NO ) {
[self stopBarberPole];
}
}
}
- (void)startBarberPole
{
self.barberPoleView.hidden = NO;
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"position.x"];
theAnimation.duration=0.5f;
theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=NO;
theAnimation.fromValue=[NSNumber numberWithFloat:-self.barberPoleStripWidth*2];
theAnimation.toValue=[NSNumber numberWithFloat:0.0];
[self.replicatorLayer addAnimation:theAnimation forKey:@"animatePosition"];
}
- (void)stopBarberPole
{
self.barberPoleView.hidden = YES;
[self.replicatorLayer removeAllAnimations];
}
- (void)setBarberPoleStripWidth:(CGFloat)barberPoleStripWidth
{
_barberPoleStripWidth = barberPoleStripWidth;
[self layoutSubviews];
}
@end