root / Classes / ActivityIndicatorView.m @ 60731366
History | View | Annotate | Download (5.3 kB)
1 |
// |
---|---|
2 |
// ActivityIndicatorView.m |
3 |
// OpenStack |
4 |
// |
5 |
// Created by Mike Mayo on 10/9/10. |
6 |
// The OpenStack project is provided under the Apache 2.0 license. |
7 |
// |
8 |
|
9 |
#import "ActivityIndicatorView.h" |
10 |
#import <QuartzCore/QuartzCore.h> |
11 |
#import "AnimatedProgressView.h" |
12 |
|
13 |
#define kFadeTime 0.25 |
14 |
#define kFontSize 14.0 |
15 |
#define kCornerRadius 7 |
16 |
#define kSpinnerStyle UIActivityIndicatorViewStyleWhite |
17 |
|
18 |
@implementation ActivityIndicatorView |
19 |
|
20 |
@synthesize progressView, spinner; |
21 |
|
22 |
#pragma mark - Object lifecycle |
23 |
|
24 |
- (id)initWithFrame:(CGRect)frame text:(NSString *)text withProgress:(BOOL)withProgress { |
25 |
if ((self = [super initWithFrame:frame])) { |
26 |
// border color |
27 |
self.backgroundColor = [UIColor colorWithRed:0.498 green:0.498 blue:0.498 alpha:1.0]; |
28 |
self.layer.cornerRadius = kCornerRadius; |
29 |
|
30 |
CGRect contentFrame = CGRectInset(self.bounds, 2, 2); |
31 |
UIView *contentView = [[[UIView alloc] initWithFrame:contentFrame] autorelease]; |
32 |
contentView.backgroundColor = [UIColor colorWithRed:0.198 green:0.198 blue:0.198 alpha:1.0]; |
33 |
contentView.layer.cornerRadius = kCornerRadius; |
34 |
[self addSubview:contentView]; |
35 |
[self sendSubviewToBack:contentView]; |
36 |
|
37 |
self.spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:kSpinnerStyle] autorelease]; |
38 |
self.spinner.frame = CGRectMake(19.0, 10.0, 20.0, 20.0); |
39 |
[self.spinner startAnimating]; |
40 |
[self addSubview:self.spinner]; |
41 |
|
42 |
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(45.0, 5.0, contentFrame.size.width, 30.0)] autorelease]; |
43 |
label.textColor = [UIColor whiteColor]; |
44 |
label.backgroundColor = [UIColor clearColor]; |
45 |
label.text = text; |
46 |
label.font = [UIFont systemFontOfSize:kFontSize]; |
47 |
[self addSubview:label]; |
48 |
|
49 |
if (withProgress) { |
50 |
self.progressView = [[[AnimatedProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar] autorelease]; |
51 |
self.progressView.frame = CGRectMake(10.0, 40.0, frame.size.width - 20.0, 10.0); |
52 |
[self addSubview:progressView]; |
53 |
} |
54 |
} |
55 |
return self; |
56 |
} |
57 |
|
58 |
- (id)initWithFrame:(CGRect)frame text:(NSString *)text { |
59 |
return [self initWithFrame:frame text:text withProgress:NO]; |
60 |
} |
61 |
|
62 |
- (id)initWithText:(NSString *)text withProgress:(BOOL)withProgress { |
63 |
return [self initWithFrame:[self frameForText:text withProgress:withProgress] text:text withProgress:withProgress]; |
64 |
} |
65 |
|
66 |
- (id)initWithText:(NSString *)text { |
67 |
return [self initWithText:text withProgress:NO]; |
68 |
} |
69 |
|
70 |
+ (id)activityIndicatorViewWithText:(NSString *)text withProgress:(BOOL)withProgress { |
71 |
return [[[self alloc] initWithText:text withProgress:withProgress] autorelease]; |
72 |
} |
73 |
|
74 |
+ (id)activityIndicatorViewWithText:(NSString *)text { |
75 |
return [self activityIndicatorViewWithText:text withProgress:NO]; |
76 |
} |
77 |
|
78 |
+ (id)activityIndicatorViewWithText:(NSString *)text withProgress:(BOOL)withProgress andAddToView:(UIView *)view scrollOffset:(CGFloat)offset { |
79 |
ActivityIndicatorView *activityIndicatorView = [self activityIndicatorViewWithText:text withProgress:withProgress]; |
80 |
[activityIndicatorView addToView:view scrollOffset:offset]; |
81 |
return activityIndicatorView; |
82 |
} |
83 |
|
84 |
+ (id)activityIndicatorViewWithText:(NSString *)text withProgress:(BOOL)withProgress andAddToView:(UIView *)view { |
85 |
return [self activityIndicatorViewWithText:text withProgress:withProgress andAddToView:view scrollOffset:0.0]; |
86 |
} |
87 |
|
88 |
+ (id)activityIndicatorViewWithText:(NSString *)text andAddToView:(UIView *)view scrollOffset:(CGFloat)offset { |
89 |
return [self activityIndicatorViewWithText:text withProgress:NO andAddToView:view scrollOffset:offset]; |
90 |
} |
91 |
|
92 |
+ (id)activityIndicatorViewWithText:(NSString *)text andAddToView:(UIView *)view { |
93 |
return [self activityIndicatorViewWithText:text withProgress:NO andAddToView:view scrollOffset:0.0]; |
94 |
} |
95 |
|
96 |
#pragma mark - View lifecycle |
97 |
|
98 |
- (void)didMoveToSuperview { |
99 |
[super didMoveToSuperview]; |
100 |
self.superview.userInteractionEnabled = NO; |
101 |
} |
102 |
|
103 |
#pragma mark - Memory management |
104 |
|
105 |
- (void)dealloc { |
106 |
[progressView release]; |
107 |
[spinner release]; |
108 |
[super dealloc]; |
109 |
} |
110 |
|
111 |
#pragma mark - Internal |
112 |
|
113 |
- (CGRect)frameForText:(NSString *)text withProgress:(BOOL)withProgress { |
114 |
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:kFontSize] |
115 |
constrainedToSize:CGSizeMake(300.0f, 9000.0f) |
116 |
lineBreakMode:UILineBreakModeWordWrap]; |
117 |
CGFloat baseWidth = 62.0; |
118 |
CGFloat width = baseWidth + textSize.width; |
119 |
CGFloat x = (320.0 - width) / 2; |
120 |
return CGRectMake(x, 146.0, width, withProgress ? 60.0 : 40.0); |
121 |
} |
122 |
|
123 |
#pragma mark - Actions |
124 |
|
125 |
- (void)addToView:(UIView *)view scrollOffset:(CGFloat)offset { |
126 |
self.alpha = 0.0; |
127 |
[view addSubview:self]; |
128 |
|
129 |
self.center = view.center; |
130 |
|
131 |
// if it's a scroll view, move the frame down |
132 |
CGRect newFrame = self.frame; |
133 |
newFrame.origin.y = 146.0 + offset; |
134 |
self.frame = newFrame; |
135 |
|
136 |
[UIView animateWithDuration:kFadeTime animations:^{ |
137 |
self.alpha = 1.0; |
138 |
}]; |
139 |
} |
140 |
|
141 |
- (void)addToView:(UIView *)view { |
142 |
[self addToView:view scrollOffset:0.0]; |
143 |
} |
144 |
|
145 |
- (void)removeFromSuperview { |
146 |
[self.spinner stopAnimating]; |
147 |
[UIView animateWithDuration:kFadeTime animations:^{ |
148 |
self.alpha = 0.0; |
149 |
} completion:^(BOOL finished) { |
150 |
self.superview.userInteractionEnabled = YES; |
151 |
[super removeFromSuperview]; |
152 |
}]; |
153 |
} |
154 |
|
155 |
@end |