diff --git a/Demo/SpinKit/RTTestViewController.m b/Demo/SpinKit/RTTestViewController.m index c733d0e..3cb7f4a 100644 --- a/Demo/SpinKit/RTTestViewController.m +++ b/Demo/SpinKit/RTTestViewController.m @@ -15,7 +15,7 @@ @interface RTTestViewController () @implementation RTTestViewController --(void)insertSpinner:(RTSpinKitView*)spinner +-(void)insertSpinner:(RTSpinKitViewStyle)style atIndex:(NSInteger)index backgroundColor:(UIColor*)backgroundColor { @@ -24,46 +24,26 @@ -(void)insertSpinner:(RTSpinKitView*)spinner UIView *panel = [[UIView alloc] initWithFrame:CGRectOffset(screenBounds, screenWidth * index, 0.0)]; panel.backgroundColor = backgroundColor; - - spinner.center = CGPointMake(CGRectGetMidX(screenBounds), CGRectGetMidY(screenBounds)); - [spinner startAnimating]; - - [panel addSubview:spinner]; + + [RTSpinKitView showIn:panel withStyle:style]; // or you can use showIn:(UIView*)view withStyle:(RTSpinKitViewStyle)style andColor:(UIColor*) color UIScrollView *scrollView = (UIScrollView*)self.view; [scrollView addSubview:panel]; } -(void)loadView { + // you can set params with Appearance proxy + // [RTSpinKitView appearance].style for cross application spinner + [RTSpinKitView appearance].overlayColor = [UIColor whiteColor]; + [RTSpinKitView appearance].color = [UIColor whiteColor]; + [RTSpinKitView appearance].style = RTSpinKitViewStyleWave; + UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.pagingEnabled = YES; scrollView.alwaysBounceVertical = NO; scrollView.alwaysBounceHorizontal = YES; scrollView.backgroundColor = [UIColor darkGrayColor]; self.view = scrollView; - - [self insertSpinner:[[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStylePlane color:[UIColor whiteColor]] - atIndex:0 - backgroundColor:[UIColor colorWithRed:0.827 green:0.329 blue:0 alpha:1.0]]; - - [self insertSpinner:[[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleBounce color:[UIColor whiteColor]] - atIndex:1 - backgroundColor:[UIColor colorWithRed:0.173 green:0.243 blue:0.314 alpha:1.0]]; - - [self insertSpinner:[[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleWave color:[UIColor whiteColor]] - atIndex:2 - backgroundColor:[UIColor colorWithRed:0.102 green:0.737 blue:0.612 alpha:1.0]]; - - [self insertSpinner:[[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleWanderingCubes color:[UIColor whiteColor]] - atIndex:3 - backgroundColor:[UIColor colorWithRed:0.161 green:0.502 blue:0.725 alpha:1.0]]; - - [self insertSpinner:[[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStylePulse color:[UIColor whiteColor]] - atIndex:4 - backgroundColor:[UIColor colorWithRed:0.498 green:0.549 blue:0.553 alpha:1.0]]; - - CGRect screenBounds = [[UIScreen mainScreen] bounds]; - scrollView.contentSize = CGSizeMake(5 * CGRectGetWidth(screenBounds), CGRectGetHeight(screenBounds)); } -(UIStatusBarStyle)preferredStatusBarStyle { @@ -76,6 +56,36 @@ - (void)viewDidLoad // Do any additional setup after loading the view. } +-(void)viewDidAppear:(BOOL)animated { + [RTSpinKitView showOverlay:0.5f]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [RTSpinKitView hideOverlay]; + + [self insertSpinner:RTSpinKitViewStylePlane + atIndex:0 + backgroundColor:[UIColor colorWithRed:0.827 green:0.329 blue:0 alpha:1.0]]; + + [self insertSpinner:RTSpinKitViewStyleBounce + atIndex:1 + backgroundColor:[UIColor colorWithRed:0.173 green:0.243 blue:0.314 alpha:1.0]]; + + [self insertSpinner:RTSpinKitViewStyleWave + atIndex:2 + backgroundColor:[UIColor colorWithRed:0.102 green:0.737 blue:0.612 alpha:1.0]]; + + [self insertSpinner:RTSpinKitViewStyleWanderingCubes + atIndex:3 + backgroundColor:[UIColor colorWithRed:0.161 green:0.502 blue:0.725 alpha:1.0]]; + + [self insertSpinner:RTSpinKitViewStylePulse + atIndex:4 + backgroundColor:[UIColor colorWithRed:0.498 green:0.549 blue:0.553 alpha:1.0]]; + + CGRect screenBounds = [[UIScreen mainScreen] bounds]; + ((UIScrollView*) self.view).contentSize = CGSizeMake(5 * CGRectGetWidth(screenBounds), CGRectGetHeight(screenBounds)); + }); +} + - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; diff --git a/README.md b/README.md index c6f4ef9..fb830c1 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,22 @@ Available styles: * `RTSpinKitViewStyleWave` * `RTSpinKitViewStyleWanderingCubes` * `RTSpinKitViewStylePulse` + +You can user static helpers: + + [RTSpinKitView showIn:self.view withStyle:RTSpinKitViewStyleWave andColor:[UIColor whiteColor]]; + [RTSpinKitView hideIn:self.view]; + +Also you can set style and color across your application with Apperance proxy: + + // setup in AppDelegate + [RTSpinKitView appearance].color = [UIColor whiteColor]; + [RTSpinKitView appearance].style = RTSpinKitViewStyleWave; + ... + // use anyway one-style spinner + [RTSpinKitView showIn:self.view]; + + MBProgressHUD ------------- diff --git a/SpinKit/RTSpinKitView.h b/SpinKit/RTSpinKitView.h index 56a7eab..0d3659e 100644 --- a/SpinKit/RTSpinKitView.h +++ b/SpinKit/RTSpinKitView.h @@ -18,9 +18,36 @@ typedef NS_ENUM(NSInteger, RTSpinKitViewStyle) { @interface RTSpinKitView : UIView -@property (nonatomic, strong) UIColor *color; +@property (nonatomic, strong) UIColor *color UI_APPEARANCE_SELECTOR; +// this property is using through appearance proxy only +@property (nonatomic, strong) UIColor *overlayColor UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) RTSpinKitViewStyle style UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) BOOL hidesWhenStopped; +// show overlay with specific alpha, getting color and style from appearance proxy ++(instancetype)showOverlay:(float)alpha; + +// show overlay with specific alpha, colored and styled spinner as subview ++(instancetype)showOverlay:(float)alpha withStyle:(RTSpinKitViewStyle)style andColor:(UIColor*) color; + +// hide overlay view with spinner ++(void)hideOverlay; + +// show with settings, which you can set withing appearance proxy +// fe [RTSpinKitView appearance].style = RTSpinKitViewStyleBounce; +// [RTSpinKitView appearance].color = [UIColor whiteColor]; ++(instancetype)showIn:(UIView*)view; + +// show with color settings, which you can set withing appearance proxy +// fe [RTSpinKitView appearance].color = [UIColor whiteColor]; ++(instancetype)showIn:(UIView*)view withStyle:(RTSpinKitViewStyle)style; + +// create and show spinner in specific view ++(instancetype)showIn:(UIView*)view withStyle:(RTSpinKitViewStyle)style andColor:(UIColor*) color; + +// find and hide spinner in specific view ++(void)hideIn:(UIView*)view; + -(instancetype)initWithStyle:(RTSpinKitViewStyle)style; -(instancetype)initWithStyle:(RTSpinKitViewStyle)style color:(UIColor*)color; diff --git a/SpinKit/RTSpinKitView.m b/SpinKit/RTSpinKitView.m index 2ef37af..6359ba3 100644 --- a/SpinKit/RTSpinKitView.m +++ b/SpinKit/RTSpinKitView.m @@ -24,12 +24,84 @@ static CATransform3D RTSpinKit3DRotationWithPerspective(CGFloat perspective, } @interface RTSpinKitView () -@property (nonatomic, assign) RTSpinKitViewStyle style; @property (nonatomic, assign, getter = isStopped) BOOL stopped; @end +static int SPIN_OVERLAY_TAG = 1111; + @implementation RTSpinKitView +// show overlay with specific alpha, getting color and style from appearance proxy ++(instancetype)showOverlay:(float)alpha { + if (![RTSpinKitView appearance].color) [RTSpinKitView appearance].color = [UIColor blackColor]; + return [RTSpinKitView showOverlay:alpha withStyle:[RTSpinKitView appearance].style andColor:[RTSpinKitView appearance].color]; +} + +// show overlay with specific alpha, colored and styled spinner as subview ++(instancetype)showOverlay:(float)alpha withStyle:(RTSpinKitViewStyle)style andColor:(UIColor*) color { + UIWindow* window = [[UIApplication sharedApplication] keyWindow]; + UIView* overlay = [UIView new]; + overlay.tag = SPIN_OVERLAY_TAG; + overlay.frame = window.frame; + overlay.alpha = alpha; + UIColor* overlayColor = [RTSpinKitView appearance].overlayColor; + if (!overlayColor) overlayColor = [UIColor blackColor]; + // get color from appearance proxy + overlay.backgroundColor = overlayColor; + [window addSubview:overlay]; + // add as window child to prevent alpha influence + return [RTSpinKitView showIn:window]; +} + +// show with settings, which you can set withing appearance proxy +// fe [RTSpinKitView appearance].style = RTSpinKitViewStyleBounce; +// [RTSpinKitView appearance].color = [UIColor whiteColor]; ++(instancetype)showIn:(UIView*)view { + if (![RTSpinKitView appearance].color) [RTSpinKitView appearance].color = [UIColor blackColor]; + return [RTSpinKitView showIn:view + withStyle:[RTSpinKitView appearance].style + andColor:[RTSpinKitView appearance].color]; +} + +// show with color settings, which you can set withing appearance proxy +// fe [RTSpinKitView appearance].color = [UIColor whiteColor]; ++(instancetype)showIn:(UIView*)view withStyle:(RTSpinKitViewStyle)style { + if (![RTSpinKitView appearance].color) [RTSpinKitView appearance].color = [UIColor blackColor]; + return [RTSpinKitView showIn:view + withStyle:style + andColor:[RTSpinKitView appearance].color]; +} + +// create and show spinner in specific view ++(instancetype)showIn:(UIView*)view withStyle:(RTSpinKitViewStyle)style andColor:(UIColor*) color { + RTSpinKitView* spinner = [[RTSpinKitView alloc] initWithStyle:style color:color]; + // show spinner in the superview center + spinner.center = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)); + spinner.hidesWhenStopped = YES; + [spinner startAnimating]; + [view addSubview:spinner]; + return spinner; +} + +// hide overlay view with spinner ++(void)hideOverlay { + UIWindow* window = [[UIApplication sharedApplication] keyWindow]; + for (UIView* sub in window.subviews) { + if (sub.tag == SPIN_OVERLAY_TAG || [sub isKindOfClass:[RTSpinKitView class]]) { + [sub removeFromSuperview]; + } + } +} + +// find and hide spinner in specific view ++(void)hideIn:(UIView*) view { + for (UIView* sub in view.subviews) { + if ([sub isKindOfClass:[RTSpinKitView class]]) { + [sub removeFromSuperview]; + } + } +} + -(instancetype)initWithStyle:(RTSpinKitViewStyle)style { return [self initWithStyle:style color:[UIColor grayColor]]; } @@ -38,7 +110,7 @@ -(instancetype)initWithStyle:(RTSpinKitViewStyle)style color:(UIColor*)color { self = [super init]; if (self) { _style = style; - _color = color; + [self setColor:color]; _hidesWhenStopped = YES; [self sizeToFit];