-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDBLoadingView.m
More file actions
166 lines (134 loc) · 5.2 KB
/
DBLoadingView.m
File metadata and controls
166 lines (134 loc) · 5.2 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// DBLoadingView.m
// DropboxSDK
//
// Created by Brian Smith on 6/30/10.
// Copyright 2010 Dropbox, Inc. All rights reserved.
//
#import "DBLoadingView.h"
#define kPadding 10
@interface DBLoadingView ()
- (CGRect)beveledBoxFrame;
@end
@implementation DBLoadingView
- (id)init {
return [self initWithTitle:nil];
}
- (id)initWithTitle:(NSString*)theTitle {
CGRect frame = [[UIApplication sharedApplication] keyWindow].frame;
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
activityIndicator =
[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self addSubview:activityIndicator];
imageView = [[UIImageView alloc] init];
[self addSubview:imageView];
titleLabel = [UILabel new];
titleLabel.text = theTitle;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:titleLabel];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGRect contentFrame = [self beveledBoxFrame];
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat fillColor[] = { 0, 0, 0, 128.0/255 };
CGContextSetFillColor(context, fillColor);
CGFloat radius = 6;
CGContextMoveToPoint(context, contentFrame.origin.x + radius, contentFrame.origin.y);
CGContextAddArcToPoint(context,
CGRectGetMaxX(contentFrame), contentFrame.origin.y,
CGRectGetMaxX(contentFrame), CGRectGetMaxY(contentFrame), radius);
CGContextAddArcToPoint(context,
CGRectGetMaxX(contentFrame), CGRectGetMaxY(contentFrame),
contentFrame.origin.x, CGRectGetMaxY(contentFrame), radius);
CGContextAddArcToPoint(context,
contentFrame.origin.x, CGRectGetMaxY(contentFrame),
contentFrame.origin.x, contentFrame.origin.y, radius);
CGContextAddArcToPoint(context,
contentFrame.origin.x, contentFrame.origin.y,
CGRectGetMaxX(contentFrame), contentFrame.origin.y, radius);
CGContextClosePath(context);
CGContextFillPath(context);
}
- (void)layoutSubviews {
CGRect contentFrame = [self beveledBoxFrame];
activityIndicator.center = CGPointMake(
floor(contentFrame.origin.x + contentFrame.size.width/2),
floor(contentFrame.origin.y + contentFrame.size.height/2) - kPadding);
CGFloat titleLeading = titleLabel.font.leading;
CGRect titleFrame = CGRectMake(
contentFrame.origin.x + kPadding,
CGRectGetMaxY(contentFrame) - 2*kPadding - titleLeading,
contentFrame.size.width - 2*kPadding, titleLeading);
titleLabel.frame = titleFrame;
CGRect imageFrame = imageView.frame;
imageFrame.origin.x = contentFrame.origin.x + floor(contentFrame.size.width/2 - imageFrame.size.width/2);
imageFrame.origin.y = contentFrame.origin.y + floor(contentFrame.size.height/2 - imageFrame.size.height/2);
imageView.frame = imageFrame;
}
- (void)dealloc {
[activityIndicator release];
[imageView release];
[titleLabel release];
[super dealloc];
}
- (void)setImage:(UIImage*)image {
imageView.image = image;
[imageView sizeToFit];
[self setNeedsLayout];
}
- (void)setOrientation:(UIInterfaceOrientation)newOrientation {
if (newOrientation == orientation) return;
orientation = newOrientation;
if (orientation == UIInterfaceOrientationPortrait) {
self.transform = CGAffineTransformIdentity;
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
self.transform = CGAffineTransformMakeRotation(M_PI);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
self.transform = CGAffineTransformMakeRotation(M_PI/2);
} else {
self.transform = CGAffineTransformMakeRotation(-M_PI/2);
}
}
- (void)show {
if (!imageView.image) {
// Only show activity indicator when we don't have an image
[activityIndicator startAnimating];
}
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
self.frame = window.frame;
[window addSubview:self];
}
- (void)finishDismiss {
[activityIndicator stopAnimating];
[self removeFromSuperview];
}
- (void)dismissAnimated:(BOOL)animated {
if (!animated) {
[self finishDismiss];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(finishDismiss)];
self.alpha = 0;
[UIView commitAnimations];
}
}
- (CGRect)beveledBoxFrame {
CGSize contentSize = self.bounds.size;
CGSize boxSize = CGSizeMake(160, 160);
CGFloat yOffset = UIInterfaceOrientationIsPortrait(orientation) ? 18 : 0;
return CGRectMake(
floor(contentSize.width/2 - boxSize.width/2),
floor(contentSize.height/2 - boxSize.height/2) + yOffset,
boxSize.width, boxSize.height);
}
@end