-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathGmailLikeLoadingView.m
More file actions
338 lines (289 loc) · 13.1 KB
/
Copy pathGmailLikeLoadingView.m
File metadata and controls
338 lines (289 loc) · 13.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
// ActivityIndicatorView.m
// ActivityIndicator
//
// Created by Nikhil Gohil on 27/12/12.
// Copyright (c) 2012 Nikhil Gohil. All rights reserved.
//
#import "GmailLikeLoadingView.h"
#import <QuartzCore/QuartzCore.h>
typedef enum {
kFlipStop = 0,
kFlipTopBottom,
kFlipBottomTop,
kFlipLeftRight,
kFlipRightLeft,
kFlipStopAnimating
} kFlipDirectionState;
@interface GmailLikeLoadingView (){
kFlipDirectionState flipState;
kFlipDirectionState previousFlipState;
UIView *frontLayerView;
UIView *backLayerView;
UIView *firstHalfFrontLayerView;
UIView *secondHalfFrontLayerView;
UIView *firstHalfBackLayerView;
UIView *secondHalfBackLayerView;
BOOL horizontal;
NSMutableArray *colorsArray;
}
-(void)animateView;
-(NSArray*)splitViewToImages:(UIView*)view forFlipState:(kFlipDirectionState)flipDirection;
@end
@implementation GmailLikeLoadingView
@synthesize isAnimating;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
CGFloat diameter = MIN(self.frame.size.width, self.frame.size.height);
colorsArray = [NSMutableArray arrayWithObjects:[UIColor colorWithRed:0.0/255.0f green:147.0/255.0f blue:78.0/255.0f alpha:1.0],[UIColor colorWithRed:20.0/255.0f green:99.0/255.0f blue:233.0/255.0 alpha:1.0],[UIColor colorWithRed:255.0/255.0f green:199.0/255.0f blue:12.0/255.0f alpha:1.0],[UIColor colorWithRed:221.0/255.0f green:0.0/255.0f blue:31.0/255.0 alpha:1.0], nil];
frontLayerView = [[UIView alloc] init];
[frontLayerView setBackgroundColor:[UIColor clearColor]];
[frontLayerView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
frontLayerView.layer.cornerRadius = diameter/2;
UIColor *frontColor = [colorsArray objectAtIndex:0];
[self moveObjectsInArray];
[frontLayerView.layer setBackgroundColor:frontColor.CGColor];
frontLayerView.center = self.center;
backLayerView = [[UIView alloc] init];
[backLayerView setBackgroundColor:[UIColor clearColor]];
[backLayerView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
backLayerView.layer.cornerRadius = diameter/2;
UIColor *backColor = [colorsArray objectAtIndex:0];
[self moveObjectsInArray];
[backLayerView.layer setBackgroundColor:backColor.CGColor];
backLayerView.center = self.center;
previousFlipState = kFlipStop;
flipState = kFlipBottomTop;
isAnimating = NO;
horizontal = NO;
}
return self;
}
-(NSArray*)splitViewToImages:(UIView*)view forFlipState:(kFlipDirectionState)flipDirection{
NSArray *returnArray = nil;
UIGraphicsBeginImageContextWithOptions(view.layer.bounds.size, view.layer.opaque, 0.f);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize size;
if (flipDirection == kFlipBottomTop || flipDirection == kFlipTopBottom) {
size = CGSizeMake(renderedImage.size.width, renderedImage.size.height / 2);
}else{
size = CGSizeMake(renderedImage.size.width / 2, renderedImage.size.height);
}
UIImage *top = nil;
UIImage *bottom = nil;
UIGraphicsBeginImageContextWithOptions(size, view.layer.opaque, 0.f);
{{
[renderedImage drawAtPoint:CGPointZero];
top = UIGraphicsGetImageFromCurrentImageContext();
}}
UIGraphicsEndImageContext();
UIGraphicsBeginImageContextWithOptions(size, view.layer.opaque, 0.f);
{{
if (flipDirection == kFlipBottomTop || flipDirection == kFlipTopBottom) {
[renderedImage drawAtPoint:CGPointMake(CGPointZero.x, -renderedImage.size.height / 2)];
}else{
[renderedImage drawAtPoint:CGPointMake(-renderedImage.size.width / 2,CGPointZero.y)];
}
bottom = UIGraphicsGetImageFromCurrentImageContext();
}}
UIGraphicsEndImageContext();
UIImageView *topHalfView = [[UIImageView alloc] initWithImage:top];
UIImageView *bottomHalfView = [[UIImageView alloc] initWithImage:bottom];
if (flipDirection == kFlipBottomTop || flipDirection == kFlipTopBottom) {
returnArray = [NSArray arrayWithObjects:topHalfView, bottomHalfView, nil];
}else{
returnArray = [NSArray arrayWithObjects:topHalfView, bottomHalfView, nil];
}
return returnArray;
}
- (CGPoint)center:(CGPoint)oldCenter movedFromAnchorPoint:(CGPoint)oldAnchorPoint toAnchorPoint:(CGPoint)newAnchorPoint withFrame:(CGRect)frame;
{
CGPoint anchorPointDiff = CGPointMake(newAnchorPoint.x - oldAnchorPoint.x, newAnchorPoint.y - oldAnchorPoint.y);
CGPoint newCenter = CGPointMake(oldCenter.x + (anchorPointDiff.x * frame.size.width),
oldCenter.y + (anchorPointDiff.y * frame.size.height));
return newCenter;
}
-(void)animateView{
NSArray *frontImages = [self splitViewToImages:frontLayerView forFlipState:flipState];
firstHalfFrontLayerView = [frontImages objectAtIndex:0];
secondHalfFrontLayerView = [frontImages objectAtIndex:1];
[firstHalfFrontLayerView setFrame:CGRectMake(0, 0, firstHalfFrontLayerView.frame.size.width, firstHalfFrontLayerView.frame.size.height)];
firstHalfFrontLayerView.frame = CGRectOffset(firstHalfFrontLayerView.frame, 0, 0);
[self addSubview:firstHalfFrontLayerView];
secondHalfFrontLayerView.frame = firstHalfFrontLayerView.frame;
if (flipState == kFlipBottomTop || flipState == kFlipTopBottom) {
secondHalfFrontLayerView.frame = CGRectOffset(secondHalfFrontLayerView.frame, 0.f, firstHalfFrontLayerView.frame.size.height);
}else{
secondHalfFrontLayerView.frame = CGRectOffset(secondHalfFrontLayerView.frame, firstHalfFrontLayerView.frame.size.width, 0.f);
}
[self addSubview:secondHalfFrontLayerView];
[frontLayerView removeFromSuperview];
NSArray *backImages = [self splitViewToImages:backLayerView forFlipState:flipState];
firstHalfBackLayerView = [backImages objectAtIndex:0];
secondHalfBackLayerView = [backImages objectAtIndex:1];
firstHalfBackLayerView.frame = firstHalfFrontLayerView.frame;
[self insertSubview:firstHalfBackLayerView belowSubview:firstHalfFrontLayerView];
secondHalfBackLayerView.frame = secondHalfFrontLayerView.frame;
[self insertSubview:secondHalfBackLayerView belowSubview:secondHalfFrontLayerView];
CATransform3D skewedIdentityTransform = CATransform3DIdentity;
float zDistance = 1000.000000;
skewedIdentityTransform.m34 = 1.0 / -zDistance;
CGPoint newTopViewAnchorPoint;
CGPoint newAnchorPointBottomHalf;
float x,y,z;
if (flipState == kFlipBottomTop || flipState == kFlipTopBottom) {
newTopViewAnchorPoint = CGPointMake(0.5, 1.0);
newAnchorPointBottomHalf = CGPointMake(0.5f, 0.f);
x = 1.f;
y = 0.f;
z = 0.f;
}else{
newTopViewAnchorPoint = CGPointMake(1.0f, 0.5f);
newAnchorPointBottomHalf = CGPointMake(0.f,0.5f);
x = 0.f;
y = 1.f;
z = 0.f;
}
CGPoint newTopViewCenter = [self center:firstHalfFrontLayerView.center movedFromAnchorPoint:firstHalfFrontLayerView.layer.anchorPoint toAnchorPoint:newTopViewAnchorPoint withFrame:firstHalfFrontLayerView.frame];
firstHalfFrontLayerView.layer.anchorPoint = newTopViewAnchorPoint;
if (flipState == kFlipBottomTop || flipState == kFlipTopBottom) {
firstHalfFrontLayerView.center = newTopViewCenter;
}else{
[firstHalfFrontLayerView setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
}
[[firstHalfFrontLayerView layer] setOpacity:1.0f];
[[firstHalfFrontLayerView layer] setOpaque:YES];
CABasicAnimation *topAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
topAnim.beginTime = CACurrentMediaTime();
topAnim.duration = 0.5;
topAnim.fromValue = [NSValue valueWithCATransform3D:skewedIdentityTransform];
if (flipState == kFlipBottomTop || flipState == kFlipTopBottom) {
topAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(skewedIdentityTransform, -M_PI_2, x, y, z)];
}else{
topAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(skewedIdentityTransform, M_PI_2, x, y, z)];
}
topAnim.delegate = self;
topAnim.removedOnCompletion = NO;
topAnim.fillMode = kCAFillModeForwards;
topAnim.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.70 :0.00 :1.00 :1.00];
[[firstHalfFrontLayerView layer] setOpacity:0.955555f];
[[firstHalfFrontLayerView layer] setOpaque:YES];
[firstHalfFrontLayerView.layer addAnimation:topAnim forKey:@"topDownFlip"];
CGPoint newBottomHalfCenter = [self center:secondHalfBackLayerView.center movedFromAnchorPoint:secondHalfBackLayerView.layer.anchorPoint toAnchorPoint:newAnchorPointBottomHalf withFrame:secondHalfBackLayerView.frame];
secondHalfBackLayerView.layer.anchorPoint = newAnchorPointBottomHalf;
if (flipState == kFlipBottomTop || flipState == kFlipTopBottom) {
secondHalfBackLayerView.center = newBottomHalfCenter;
}else{
[secondHalfBackLayerView setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
}
[[secondHalfBackLayerView layer] setOpacity:1.0f];
[[secondHalfBackLayerView layer] setOpaque:YES];
CABasicAnimation *bottomAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
bottomAnim.beginTime = topAnim.beginTime + topAnim.duration;
bottomAnim.duration = topAnim.duration;
if (flipState == kFlipBottomTop || flipState == kFlipTopBottom) {
bottomAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(skewedIdentityTransform, M_PI_2, x, y, z)];
}else{
bottomAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(skewedIdentityTransform, -M_PI_2, x, y, z)];
}
bottomAnim.toValue = [NSValue valueWithCATransform3D:skewedIdentityTransform];
bottomAnim.delegate = self;
bottomAnim.removedOnCompletion = NO;
bottomAnim.fillMode = kCAFillModeBoth;
bottomAnim.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.30 :1.00 :1.00 :1.00];
[[secondHalfBackLayerView layer] setOpacity:0.955555f];
[[secondHalfBackLayerView layer] setOpaque:YES];
[secondHalfBackLayerView.layer addAnimation:bottomAnim forKey:@"bottomDownFlip"];
}
-(void)checkFlipDirectionState {
switch (flipState) {
case kFlipBottomTop:
{
horizontal = NO;
[self animateView];
previousFlipState = kFlipBottomTop;
flipState = kFlipTopBottom;
}
break;
case kFlipTopBottom:
{
horizontal = NO;
[secondHalfBackLayerView.superview bringSubviewToFront:secondHalfBackLayerView];
previousFlipState = kFlipTopBottom;
flipState = kFlipStop;
}
break;
case kFlipLeftRight:
{
horizontal = YES;
[self animateView];
previousFlipState = kFlipLeftRight;
flipState = kFlipRightLeft;
}
break;
case kFlipRightLeft:
{
horizontal = YES;
[secondHalfBackLayerView.superview bringSubviewToFront:secondHalfBackLayerView];
previousFlipState = kFlipRightLeft;
flipState = kFlipStop;
}
break;
case kFlipStop:
{
[firstHalfFrontLayerView removeFromSuperview];
[secondHalfFrontLayerView removeFromSuperview];
[firstHalfBackLayerView removeFromSuperview];
[secondHalfBackLayerView removeFromSuperview];
firstHalfFrontLayerView = secondHalfFrontLayerView = firstHalfBackLayerView = secondHalfBackLayerView = nil;
CGColorRef color = frontLayerView.layer.backgroundColor;
frontLayerView.layer.backgroundColor = backLayerView.layer.backgroundColor;
backLayerView.layer.backgroundColor = color;
if (horizontal == NO) {
flipState = kFlipLeftRight;
}else{
flipState = kFlipBottomTop;
}
UIColor *backColor = [colorsArray objectAtIndex:0];
[self moveObjectsInArray];
backLayerView.layer.backgroundColor = backColor.CGColor;
[self checkFlipDirectionState];
}
break;
default:
{
[firstHalfFrontLayerView removeFromSuperview];
[secondHalfFrontLayerView removeFromSuperview];
[firstHalfBackLayerView removeFromSuperview];
[secondHalfBackLayerView removeFromSuperview];
firstHalfFrontLayerView = secondHalfFrontLayerView = firstHalfBackLayerView = secondHalfBackLayerView = nil;
}
break;
}
}
-(void)stopAnimating{
flipState = kFlipStopAnimating;
isAnimating = NO;
[self checkFlipDirectionState];
}
-(void)startAnimating{
flipState = kFlipBottomTop;
isAnimating = YES;
[self checkFlipDirectionState];
}
#pragma mark - CAAnimation delegate callbacks
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
{
[self checkFlipDirectionState];
}
- (void)moveObjectsInArray{
id obj = [colorsArray objectAtIndex:0];
[colorsArray removeObjectAtIndex:0];
[colorsArray addObject:obj];
}
@end