Permalink
Browse files

add showSpinner / hideSpinner helpers for any UIView

  • Loading branch information...
1 parent f649084 commit 3fcdfe3149617ab7cf443ddc3454d4a003b29bcd @ettore committed Oct 25, 2013
Showing with 50 additions and 1 deletion.
  1. +16 −0 categories/CLCGUIViewCategory.h
  2. +34 −1 categories/CLCGUIViewCategory.m
@@ -108,4 +108,20 @@
- (CGFloat)centerY;
- (void)setCenterY:(CGFloat)centerY;
+/*!
+ Creates and adds a spinner to the center of the view. A pre-existing spinner
+ can be passed in and in that case it will be used, otherwise a new one will be
+ created. The spinner will then be added as a subview, centered horizontally
+ and vertically.
+
+ @param spinner The spinner to be used; if nil, a new spinner will be created.
+ */
+-(UIActivityIndicatorView*)showSpinner:(UIActivityIndicatorView*)spinner;
+
+/*!
+ Removes the given spinner from the subviews of this view.
+ @param spinner Spinner to be removed. No-op if spinner is nil.
+ */
+-(void)hideSpinner:(UIActivityIndicatorView*)spinner;
+
@end
@@ -27,7 +27,7 @@
#import "CLCGUIViewCategory.h"
-
+#import "clcg_gfx.h"
@implementation UIView (Candygirl)
@@ -233,6 +233,39 @@ -(void)setCenterY:(CGFloat)centerY
}
+-(UIActivityIndicatorView*)showSpinner:(UIActivityIndicatorView*)spinner
+{
+ // if spinner is already a subview, no need to add it (or create it)
+ if (![[self subviews] containsObject:spinner]) {
+ if (nil == spinner) {
+ spinner = [[UIActivityIndicatorView alloc]
+ initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
+ [spinner autorelease];
+ }
+
+ // the spinner is not added to the view yet, so add it
+ [self addSubview:spinner];
+ }
+
+ [spinner centerHorizontally];
+ [spinner centerVertically];
+ [self setNeedsLayout];
+
+ // go! and make sure to show it
+ [spinner startAnimating];
+ [self bringSubviewToFront:spinner];
+
+ return spinner;
+}
+
+
+-(void)hideSpinner:(UIActivityIndicatorView*)spinner
+{
+ [spinner stopAnimating];
+ clcg_safe_remove_from_superview(spinner);
+}
+
+
-(UIView*)findFirstResponder
{
if (self.isFirstResponder) {

0 comments on commit 3fcdfe3

Please sign in to comment.