Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 39 additions & 29 deletions Demo/SpinKit/RTTestViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @interface RTTestViewController ()

@implementation RTTestViewController

-(void)insertSpinner:(RTSpinKitView*)spinner
-(void)insertSpinner:(RTSpinKitViewStyle)style
atIndex:(NSInteger)index
backgroundColor:(UIColor*)backgroundColor
{
Expand All @@ -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 {
Expand All @@ -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];
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
29 changes: 28 additions & 1 deletion SpinKit/RTSpinKitView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
76 changes: 74 additions & 2 deletions SpinKit/RTSpinKitView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}
Expand All @@ -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];
Expand Down