// // ActivityIndicatorView.m // OpenStack // // Created by Mike Mayo on 10/9/10. // The OpenStack project is provided under the Apache 2.0 license. // #import "ActivityIndicatorView.h" #import #import "AnimatedProgressView.h" #define kFadeTime 0.25 #define kFontSize 14.0 #define kCornerRadius 7 #define kSpinnerStyle UIActivityIndicatorViewStyleWhite @implementation ActivityIndicatorView @synthesize progressView, spinner; #pragma mark - Object lifecycle - (id)initWithFrame:(CGRect)frame text:(NSString *)text withProgress:(BOOL)withProgress { if ((self = [super initWithFrame:frame])) { // border color self.backgroundColor = [UIColor colorWithRed:0.498 green:0.498 blue:0.498 alpha:1.0]; self.layer.cornerRadius = kCornerRadius; CGRect contentFrame = CGRectInset(self.bounds, 2, 2); UIView *contentView = [[[UIView alloc] initWithFrame:contentFrame] autorelease]; contentView.backgroundColor = [UIColor colorWithRed:0.198 green:0.198 blue:0.198 alpha:1.0]; contentView.layer.cornerRadius = kCornerRadius; [self addSubview:contentView]; [self sendSubviewToBack:contentView]; self.spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:kSpinnerStyle] autorelease]; self.spinner.frame = CGRectMake(19.0, 10.0, 20.0, 20.0); [self.spinner startAnimating]; [self addSubview:self.spinner]; UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(45.0, 5.0, contentFrame.size.width, 30.0)] autorelease]; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor clearColor]; label.text = text; label.font = [UIFont systemFontOfSize:kFontSize]; [self addSubview:label]; if (withProgress) { self.progressView = [[[AnimatedProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar] autorelease]; self.progressView.frame = CGRectMake(10.0, 40.0, frame.size.width - 20.0, 10.0); [self addSubview:progressView]; } } return self; } - (id)initWithFrame:(CGRect)frame text:(NSString *)text { return [self initWithFrame:frame text:text withProgress:NO]; } - (id)initWithText:(NSString *)text withProgress:(BOOL)withProgress { return [self initWithFrame:[self frameForText:text withProgress:withProgress] text:text withProgress:withProgress]; } - (id)initWithText:(NSString *)text { return [self initWithText:text withProgress:NO]; } + (id)activityIndicatorViewWithText:(NSString *)text withProgress:(BOOL)withProgress { return [[[self alloc] initWithText:text withProgress:withProgress] autorelease]; } + (id)activityIndicatorViewWithText:(NSString *)text { return [self activityIndicatorViewWithText:text withProgress:NO]; } + (id)activityIndicatorViewWithText:(NSString *)text withProgress:(BOOL)withProgress andAddToView:(UIView *)view scrollOffset:(CGFloat)offset { ActivityIndicatorView *activityIndicatorView = [self activityIndicatorViewWithText:text withProgress:withProgress]; [activityIndicatorView addToView:view scrollOffset:offset]; return activityIndicatorView; } + (id)activityIndicatorViewWithText:(NSString *)text withProgress:(BOOL)withProgress andAddToView:(UIView *)view { return [self activityIndicatorViewWithText:text withProgress:withProgress andAddToView:view scrollOffset:0.0]; } + (id)activityIndicatorViewWithText:(NSString *)text andAddToView:(UIView *)view scrollOffset:(CGFloat)offset { return [self activityIndicatorViewWithText:text withProgress:NO andAddToView:view scrollOffset:offset]; } + (id)activityIndicatorViewWithText:(NSString *)text andAddToView:(UIView *)view { return [self activityIndicatorViewWithText:text withProgress:NO andAddToView:view scrollOffset:0.0]; } #pragma mark - View lifecycle - (void)didMoveToSuperview { [super didMoveToSuperview]; self.superview.userInteractionEnabled = NO; } #pragma mark - Memory management - (void)dealloc { [progressView release]; [spinner release]; [super dealloc]; } #pragma mark - Internal - (CGRect)frameForText:(NSString *)text withProgress:(BOOL)withProgress { CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:kFontSize] constrainedToSize:CGSizeMake(300.0f, 9000.0f) lineBreakMode:UILineBreakModeWordWrap]; CGFloat baseWidth = 62.0; CGFloat width = baseWidth + textSize.width; CGFloat x = (320.0 - width) / 2; return CGRectMake(x, 146.0, width, withProgress ? 60.0 : 40.0); } #pragma mark - Actions - (void)addToView:(UIView *)view scrollOffset:(CGFloat)offset { self.alpha = 0.0; [view addSubview:self]; self.center = view.center; // if it's a scroll view, move the frame down CGRect newFrame = self.frame; newFrame.origin.y = 146.0 + offset; self.frame = newFrame; [UIView animateWithDuration:kFadeTime animations:^{ self.alpha = 1.0; }]; } - (void)addToView:(UIView *)view { [self addToView:view scrollOffset:0.0]; } - (void)removeFromSuperview { [self.spinner stopAnimating]; [UIView animateWithDuration:kFadeTime animations:^{ self.alpha = 0.0; } completion:^(BOOL finished) { self.superview.userInteractionEnabled = YES; [super removeFromSuperview]; }]; } @end