-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUIView+KRFoldingViews.m
249 lines (191 loc) · 8.94 KB
/
UIView+KRFoldingViews.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
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
//
// UIView+KRFoldingViews.m
// KRFoldingViews
//
// Created by Kishyr Ramdial on 2012/08/09.
// Copyright (c) 2012 Kishyr Ramdial. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "UIView+KRFoldingViews.h"
#import <QuartzCore/QuartzCore.h>
// Set the animation speed
#define ANIMATION_SPEED 1.5
@implementation UIView (KRFoldingViews)
// Basic Animations
- (CABasicAnimation *)origamiFoldLeft {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
[animation setFromValue:[NSNumber numberWithFloat:180/(180/M_PI)]];
[animation setToValue:[NSNumber numberWithFloat:0]];
[animation setDuration:ANIMATION_SPEED];
return animation;
}
- (CABasicAnimation *)origamiFoldRight {
CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
[animation2 setFromValue:[NSNumber numberWithFloat:-180/(180/M_PI)]];
[animation2 setToValue:[NSNumber numberWithFloat:0]];
[animation2 setDuration:ANIMATION_SPEED];
return animation2;
}
- (CABasicAnimation *)origamiFoldDown {
CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
[animation3 setFromValue:[NSNumber numberWithFloat:-180/(180/M_PI)]];
[animation3 setToValue:[NSNumber numberWithFloat:0]];
[animation3 setDuration:ANIMATION_SPEED];
return animation3;
}
- (CABasicAnimation *)origamiFoldUp {
CABasicAnimation *animation4 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
[animation4 setFromValue:[NSNumber numberWithFloat:180/(180/M_PI)]];
[animation4 setToValue:[NSNumber numberWithFloat:0]];
[animation4 setDuration:ANIMATION_SPEED];
return animation4;
}
- (CABasicAnimation *)scaleDown {
CABasicAnimation *animation4 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation4 setFromValue:[NSNumber numberWithFloat:1.5]];
[animation4 setToValue:[NSNumber numberWithFloat:1.0]];
[animation4 setDuration:ANIMATION_SPEED/2];
return animation4;
}
- (CABasicAnimation *)fadeIn {
CABasicAnimation *animation4 = [CABasicAnimation animationWithKeyPath:@"opacity"];
[animation4 setFromValue:[NSNumber numberWithFloat:0.5]];
[animation4 setToValue:[NSNumber numberWithFloat:1]];
[animation4 setDuration:ANIMATION_SPEED/2];
return animation4;
}
// Drop-in effect
- (void)dropIn {
[self.layer addAnimation:[self scaleDown] forKey:@"scale"];
[self.layer addAnimation:[self fadeIn] forKey:@"opacity"];
}
- (void)dropIn:(void (^)(void))completionBlock {
[self dropIn];
double delayInSeconds = ANIMATION_SPEED/2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
completionBlock();
});
}
// Fold Right
- (void)foldRightFromView:(UIView *)originalView {
self.hidden = NO;
self.layer.anchorPoint = CGPointMake(0, 0.5);
self.frame = CGRectMake(CGRectGetMaxX(originalView.frame), CGRectGetMinY(originalView.frame), CGRectGetWidth(originalView.frame), CGRectGetHeight(originalView.frame));
[self.layer addAnimation:[self origamiFoldLeft] forKey:@"transform.rotation.y"];
}
- (void)foldRightFromView:(UIView *)originalView completion:(void (^)(UIView *previousView))completionBlock {
[self foldRightFromView:originalView];
double delayInSeconds = ANIMATION_SPEED;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
completionBlock(self);
});
}
// Fold Left
- (void)foldLeftFromView:(UIView *)originalView {
self.hidden = NO;
self.layer.anchorPoint = CGPointMake(1, 0.5);
self.frame = CGRectMake(CGRectGetMinX(originalView.frame)-CGRectGetWidth(originalView.frame), CGRectGetMinY(originalView.frame), CGRectGetWidth(originalView.frame), CGRectGetHeight(originalView.frame));
[self.layer addAnimation:[self origamiFoldRight] forKey:@"transform.rotation.y"];
}
- (void)foldLeftFromView:(UIView *)originalView completion:(void (^)(UIView *previousView))completionBlock {
[self foldLeftFromView:originalView];
double delayInSeconds = ANIMATION_SPEED;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
completionBlock(self);
});
}
// Fold Down
- (void)foldDownFromView:(UIView *)originalView {
self.hidden = NO;
self.layer.anchorPoint = CGPointMake(0.5, 0);
self.frame = CGRectMake(CGRectGetMinX(originalView.frame), CGRectGetMaxY(originalView.frame), CGRectGetWidth(originalView.frame), CGRectGetHeight(originalView.frame));;
[self.layer addAnimation:[self origamiFoldDown] forKey:@"transform.rotation.x"];
}
- (void)foldDownFromView:(UIView *)originalView completion:(void (^)(UIView *previousView))completionBlock {
[self foldDownFromView:originalView];
double delayInSeconds = ANIMATION_SPEED;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
completionBlock(self);
});
}
// Fold Up
- (void)foldUpFromView:(UIView *)originalView {
self.hidden = NO;
self.layer.anchorPoint = CGPointMake(0.5, 1);
self.frame = CGRectMake(CGRectGetMinX(originalView.frame), CGRectGetMinY(originalView.frame)-CGRectGetHeight(originalView.frame), CGRectGetWidth(originalView.frame), CGRectGetHeight(originalView.frame));
[self.layer addAnimation:[self origamiFoldUp] forKey:@"transform.rotation.x"];
}
- (void)foldUpFromView:(UIView *)originalView completion:(void (^)(UIView *previousView))completionBlock {
[self foldUpFromView:originalView];
double delayInSeconds = ANIMATION_SPEED;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
completionBlock(self);
});
}
// Fold all given views (in an array) in given directions (a string of "up, down, left, right"). If the number
// of directions is less than the number of views, the last direction is used for the remaining views.
+ (void)foldViews:(NSArray *)views inDirections:(NSString *)directions fromOriginalView:(UIView *)originalView {
NSArray *allDirections = [[directions lowercaseString] componentsSeparatedByString:@","];
if ([views count] != [allDirections count]) {
NSMutableArray *d_ = [NSMutableArray arrayWithCapacity:[views count]];
for (int i = 0; i < [views count]; i++) {
@try {
[d_ addObject:[allDirections objectAtIndex:i]];
}
@catch (NSException *exception) {
[d_ addObject:[allDirections lastObject]];
}
@finally {}
}
allDirections = [NSArray arrayWithArray:d_];
}
NSMutableArray *completionBlocks = [NSMutableArray arrayWithCapacity:[allDirections count]];
typedef void (^FoldingViewBlock)(void);
NSInteger counter = 1;
for (NSString *direction in [allDirections reverseObjectEnumerator]) {
NSString *d = [[direction stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] capitalizedString];
FoldingViewBlock previousCompletionBlock;
if ([completionBlocks count] > 0)
previousCompletionBlock = [completionBlocks objectAtIndex:0];
UIView *thisView = [views objectAtIndex:([views count]-counter)];
UIView *previousView;
if (thisView == [views objectAtIndex:0])
previousView = originalView;
else
previousView = [views objectAtIndex:([views count] - counter - 1)];
BOOL firstElement = thisView == [views lastObject];
NSString *selectorString = [NSString stringWithFormat:@"fold%@FromView:%@", d, (firstElement ? @"" : @"completion:")];
FoldingViewBlock completionBlock = ^{
SEL selector = NSSelectorFromString(selectorString);
if (firstElement)
[thisView performSelector:selector withObject:previousView];
else
[thisView performSelector:selector withObject:previousView withObject:previousCompletionBlock];
};
[completionBlocks insertObject:completionBlock atIndex:0];
counter++;
}
((FoldingViewBlock)[completionBlocks objectAtIndex:0])();
}
@end