forked from steipete/PSFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIViewController+FKLoading.m
150 lines (107 loc) · 5.25 KB
/
UIViewController+FKLoading.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
#import "UIViewController+FKLoading.h"
#import "NSObject+AssociatedObjects.h"
#define kFKActivityViewMaxSize 37.f
static char activityViewKey;
static char replacedObjectKey;
static CGRect FKCenteredSquareInRectConstrainedToSize(CGRect rect, CGFloat size);
@interface UIViewController ()
@property (nonatomic, retain, readwrite) UIActivityIndicatorView *activityView;
@property (nonatomic, retain) id replacedObject;
@end
@implementation UIViewController (FKLoading)
- (void)setActivityView:(UIActivityIndicatorView *)activityView {
[self associateValue:activityView withKey:&activityViewKey];
}
- (UIActivityIndicatorView *)activityView {
UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[self associatedValueForKey:&activityViewKey];
// create activityView when it is first read
if (activityView == nil) {
activityView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
self.activityView = activityView;
}
return activityView;
}
- (void)setReplacedObject:(id)replacedObject {
[self associateValue:replacedObject withKey:&replacedObjectKey];
}
- (id)replacedObject {
return [self associatedValueForKey:&replacedObjectKey];
}
- (void)showCenteredLoadingIndicator {
[self hideLoadingIndicator];
UIActivityIndicatorView *activityView = self.activityView;
// center activityView
CGRect activityFrame = activityView.frame;
activityFrame.origin.x = (self.view.bounds.size.width - activityFrame.size.width) / 2.;
activityFrame.origin.y = (self.view.bounds.size.height - activityFrame.size.height) / 2.;
activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityView.frame = CGRectIntegral(activityFrame);
activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:activityView];
[activityView startAnimating];
}
- (void)showLoadingIndicatorInsteadOfView:(UIView *)viewToReplace {
[self hideLoadingIndicator];
UIActivityIndicatorView *activityView = self.activityView;
activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
// TODO: Resizing currently seems broken in iOS 5, we maybe have to roll our own activityIndicatorView
activityView.frame = FKCenteredSquareInRectConstrainedToSize(viewToReplace.frame, kFKActivityViewMaxSize);
activityView.autoresizingMask = viewToReplace.autoresizingMask;
viewToReplace.alpha = 0.f;
self.replacedObject = viewToReplace;
[viewToReplace.superview addSubview:activityView];
[activityView startAnimating];
}
- (void)showLoadingIndicatorInNavigationBar {
[self hideLoadingIndicator];
// initing the loading view
UIActivityIndicatorView *activityView = self.activityView;
UIView *backgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 24.f, 26.f)] autorelease];
UIBarButtonItem *barButtonItemToReplace = self.navigationItem.rightBarButtonItem;
activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
activityView.autoresizingMask = UIViewAutoresizingNone;
activityView.frame = CGRectMake(0.f, 2.f, 20.f, 20.f);
[backgroundView addSubview:activityView];
self.replacedObject = barButtonItemToReplace;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backgroundView] autorelease];
[activityView startAnimating];
}
- (void)hideLoadingIndicator {
id replacedObject = self.replacedObject;
UIActivityIndicatorView *activityView = self.activityView;
// ActivityView was in NavigationBar
if ([replacedObject isKindOfClass:[UIBarButtonItem class]]) {
[activityView stopAnimating];
self.navigationItem.rightBarButtonItem = replacedObject;
return;
}
// ActivityView was displayed instead of another view
else if ([replacedObject isKindOfClass:[UIView class]]) {
[replacedObject setAlpha:1.f];
}
[activityView stopAnimating];
[activityView removeFromSuperview];
}
@end
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Helper Functions
////////////////////////////////////////////////////////////////////////
static CGRect FKCenteredSquareInRectConstrainedToSize(CGRect rect, CGFloat size) {
CGRect centeredSquare = rect;
CGFloat xInset = 0.f;
CGFloat yInset = 0.f;
// make a centered square
if (CGRectGetWidth(centeredSquare) < CGRectGetHeight(centeredSquare)) {
yInset = (CGRectGetHeight(centeredSquare) - CGRectGetWidth(centeredSquare))/2;
} else {
xInset = (CGRectGetWidth(centeredSquare) - CGRectGetHeight(centeredSquare))/2;
}
centeredSquare = CGRectInset(centeredSquare, xInset, yInset);
// limit size
if (CGRectGetWidth(centeredSquare) > size) {
centeredSquare = CGRectInset(centeredSquare, (CGRectGetWidth(centeredSquare)-size)/2,(CGRectGetHeight(centeredSquare) - size)/2);
}
// use integral coordinates to prohibit blurring
return CGRectIntegral(centeredSquare);
}