-
Notifications
You must be signed in to change notification settings - Fork 0
/
JALoadingView.m
103 lines (82 loc) · 2.86 KB
/
JALoadingView.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
//
// JALoadingView.m
//
// Created by Jens Andersson on 2013-05-16.
// Copyright (c) 2013 Jens Andersson. All rights reserved.
//
#import "JALoadingView.h"
#import <QuartzCore/QuartzCore.h>
static CGFloat side = 40;
@implementation JALoadingView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)init {
self = [super initWithFrame:CGRectMake(0, 0, side, side)];
if (self) {
self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
self.indicator.center = CGPointMake(side / 2, side / 2);
self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
self.indicator.hidesWhenStopped = YES;
[self.indicator startAnimating];
self.layer.cornerRadius = 5;
self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.4];
[self addSubview:self.indicator];
}
return self;
}
+ (JALoadingView *)shared {
static JALoadingView *_shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_shared = [[self alloc] init];
});
return _shared;
}
+ (void)startAnimating {
[[JALoadingView shared] startAnimating];
}
+ (void)stopAnimating {
[[JALoadingView shared] stopAnimating];
}
- (void)startAnimating {
[self startAnimatingInView:[self containerView]];
}
- (void)startAnimatingInView:(UIView *)parentView {
[parentView addSubview:self];
self.center = CGPointMake(parentView.bounds.size.width / 2,parentView.bounds.size.height / 2);
[self.indicator startAnimating];
}
- (void)stopAnimating {
[self removeFromSuperview];
// Fix to be able to call stopAnimating more oftan than startAnimating
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (UIView*)containerView {
return [[[[[UIApplication sharedApplication] delegate] window] rootViewController] view];
}
@end
@implementation UIView (JALoadingView)
- (void)startLoadingWithStyle:(JALoadingViewStyle)style {
JALoadingView *loader = [JALoadingView new];
loader.backgroundColor = [UIColor clearColor];
if (style == JALoadingViewStyleGrey)
loader.indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
else if (style == JALoadingViewStyleWhite)
loader.indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[loader startAnimatingInView:self];
}
- (void)stopLoading {
for (UIView *subView in self.subviews) {
if ([subView isKindOfClass:[JALoadingView class]])
[subView removeFromSuperview];
}
}
@end