-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathYFoldView.m
474 lines (353 loc) · 15.6 KB
/
YFoldView.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//
// YFoldView.m
// TalkieWinkie
//
// Created by Gérald HUARD on 20/06/12.
// Copyright (c) 2012 Gérald HUARD. All rights reserved.
//
#import "YFoldView.h"
@implementation YFoldView
@synthesize tag=_tag;
@synthesize delegate = _delegate;
@synthesize view = _view;
+(YFoldView*)create:(UIView*)view numberOfFold:(NSInteger)parts withWay:(YFoldViewWay)way{
return [[YFoldView alloc] initWithView:view withWay:way andNumberOfFolds:parts];
}
+(YFoldView*)closeView:(UIView*)view
withNumberOfFold:(NSInteger)folds
onWay:(YFoldViewWay)way
duration:(NSTimeInterval)duration
withDelegate:(id<YFoldViewDelegate>)delegate{
YFoldView *foldv=[[YFoldView alloc] initWithView:view withWay:way andNumberOfFolds:folds];
foldv.delegate=delegate;
[foldv closeAnimatedDuration:duration];
return foldv;
}
+(YFoldView*)closeView:(UIView*)view
withNumberOfFold:(NSInteger)folds
onWay:(YFoldViewWay)way
duration:(NSTimeInterval)duration{
return [YFoldView closeView:view withNumberOfFold:folds onWay:way duration:duration withDelegate:nil];
}
+(YFoldView*)foldView:(UIView*)view withNbPart:(NSInteger)folds onWay:(YFoldViewWay)way {
return [[YFoldView alloc] initWithView:view withWay:way andNumberOfFolds:folds];
}
- (id)initWithView:(UIView*)view withWay:(YFoldViewWay)way andNumberOfFolds:(NSInteger)parts
{
self = [super init];
if (self) {
_view=view;
_way=way;
_folded=NO;
_initialSizeView=_view.frame.size;
_currentSize=_initialSizeView;
_numberOfFolds=parts;
_main3DLayer=nil;
[self initialize];
}
return self;
}
-(void) initialize{
if (!_main3DLayer){
UIGraphicsBeginImageContext(_view.frame.size);
[_view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewSnapShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0/700.0;
_main3DLayer = [[CALayer layer] retain];
_main3DLayer.frame = _view.bounds;
_main3DLayer.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.0].CGColor;
_main3DLayer.sublayerTransform = transform;
[_main3DLayer setHidden:YES];
[_view.layer addSublayer:_main3DLayer];
CGFloat partHeight=_view.bounds.size.height/_numberOfFolds;
CGFloat partWidth=_view.bounds.size.width/_numberOfFolds;
NSMutableArray *tmpLayer=[NSMutableArray array];
NSMutableArray *tmpShadow=[NSMutableArray array];
for (int i=0;i<_numberOfFolds;i++){
CGRect frame=CGRectZero;
if (_way==yFoldViewBottomToTop || _way==yFoldViewTopToBottom)
frame=CGRectMake(0, i*partHeight, _view.bounds.size.width, partHeight);
if (_way==yFoldViewRightToLeft || _way==yFoldViewLeftToRight)
frame=CGRectMake(i*partWidth, 0, partWidth, _view.bounds.size.height);
CGImageRef imageCrop = CGImageCreateWithImageInRect(viewSnapShot.CGImage, frame);
CALayer *imageCroppedLayer = [CALayer layer];
imageCroppedLayer.frame=frame;
if (_way==yFoldViewBottomToTop || _way==yFoldViewTopToBottom){
imageCroppedLayer.anchorPoint=CGPointMake(0.5, (i%2==0)?0.0:1.0);
imageCroppedLayer.position=CGPointMake(imageCroppedLayer.position.x,(i%2==0)?(imageCroppedLayer.position.y-imageCroppedLayer.bounds.size.height/2):imageCroppedLayer.position.y+imageCroppedLayer.bounds.size.height/2);
}
if (_way==yFoldViewRightToLeft || _way==yFoldViewLeftToRight){
imageCroppedLayer.anchorPoint=CGPointMake((i%2==0)?0.0:1.0,0.5);
imageCroppedLayer.position=CGPointMake((i%2==0)?(imageCroppedLayer.position.x-imageCroppedLayer.bounds.size.width/2):imageCroppedLayer.position.x+imageCroppedLayer.bounds.size.width/2,imageCroppedLayer.position.y);
}
imageCroppedLayer.contents=(id)imageCrop;
[tmpLayer addObject:imageCroppedLayer];
CAGradientLayer *shadowLayer = [CAGradientLayer layer];
shadowLayer.frame = imageCroppedLayer.bounds;
shadowLayer.opacity = 0;
shadowLayer.colors = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
if (i%2) {
shadowLayer.startPoint = CGPointMake(0.5, 0);
shadowLayer.endPoint = CGPointMake(0.5, 0.5);
}
else {
shadowLayer.startPoint = CGPointMake(0.5, 0.5);
shadowLayer.endPoint = CGPointMake(0.5, 0.0);
}
shadowLayer.shouldRasterize=YES;
[imageCroppedLayer addSublayer:shadowLayer];
[tmpShadow addObject:shadowLayer];
[_main3DLayer addSublayer:imageCroppedLayer];
}
_layerFolds=[tmpLayer retain];
_shadowLayerFolds=[tmpShadow retain];
}
}
-(void) closeAnimatedDuration:(NSTimeInterval)duration{
[self closeTo:5 duration:duration];
}
-(void) closeTo:(CGFloat)limit duration:(NSTimeInterval)duration{
if (_way == yFoldViewBottomToTop || _way == yFoldViewTopToBottom){
[self animateHorizontalFoldFrom:_currentSize.height to:limit duration:duration];
NSLog(@"Horiz");
}
if (_way == yFoldViewRightToLeft || _way == yFoldViewLeftToRight){
[self animateVerticalFoldFrom:_currentSize.width to:limit duration:duration];
NSLog(@"Vertic %f => %f",_currentSize.width,limit);
}
}
-(void) openAnimatedDuration:(NSTimeInterval)duration{
if (_way == yFoldViewBottomToTop || _way == yFoldViewTopToBottom){
[self animateHorizontalFoldFrom:_currentSize.height to:_initialSizeView.height duration:duration];
}
if (_way == yFoldViewRightToLeft || _way == yFoldViewLeftToRight){
[self animateVerticalFoldFrom:_currentSize.width to:_initialSizeView.width duration:duration];
}
}
-(void) horizontalFoldWithHeight:(CGFloat)height{
[self horizontalFoldWithHeight:height andForce:NO];
}
-(void) horizontalFoldWithHeight:(CGFloat)height andForce:(BOOL)force{
[self initialize];
if (height+10>=_initialSizeView.height)height=_initialSizeView.height;
if (_currentSize.height!=height || force){
_currentSize.height=height;
if (_currentSize.height<=_initialSizeView.height){
if (_way==yFoldViewBottomToTop || _way==yFoldViewTopToBottom){
for (int i=0;i<[_layerFolds count];i++){
[self setPropertyToLayer:[_layerFolds objectAtIndex:i] withShadow:[_shadowLayerFolds objectAtIndex:i]
withHeight:height
ofIdx:i];
}
}
}
if (_delegate && [_delegate respondsToSelector:@selector(yfoldview:view:heightChanged:)]){
[_delegate yfoldview:self view:_view heightChanged:height];
}
if (_delegate && [_delegate respondsToSelector:@selector(yfoldview:view:sizeChanged:)]){
[_delegate yfoldview:self view:_view sizeChanged:_currentSize];
}
}
if (height==_initialSizeView.height){
[self folded:NO];
}
if (height<_initialSizeView.height){
[self folded:YES];
}
}
-(void) verticalFoldWithWidth:(CGFloat)width{
[self verticalFoldWithWidth:width andForce:NO];
}
-(void) verticalFoldWithWidth:(CGFloat)width andForce:(BOOL)force{
[self initialize];
if (width+10>=_initialSizeView.width)width=_initialSizeView.width;
if (_currentSize.width!=width || force){
_currentSize.width=width;
if (_currentSize.width<=_initialSizeView.width){
if (_way==yFoldViewRightToLeft || _way==yFoldViewLeftToRight){
for (int i=0;i<[_layerFolds count];i++){
[self setPropertyToLayer:[_layerFolds objectAtIndex:i] withShadow:[_shadowLayerFolds objectAtIndex:i]
withWidth:width
ofIdx:i];
}
}
}
if (_delegate && [_delegate respondsToSelector:@selector(yfoldview:view:widthChanged:)]){
[_delegate yfoldview:self view:_view widthChanged:width];
}
if (_delegate && [_delegate respondsToSelector:@selector(yfoldview:view:sizeChanged:)]){
[_delegate yfoldview:self view:_view sizeChanged:_currentSize];
}
}
if (width==_initialSizeView.width){
[self folded:NO];
}
if (width<_initialSizeView.width){
[self folded:YES];
}
}
-(void) setEndPointPosition:(CGPoint)point{
BOOL opened=NO;
BOOL closed=NO;
if (_way==yFoldViewBottomToTop || _way==yFoldViewTopToBottom){
float height=0;
if (_way==yFoldViewBottomToTop)height=point.y-_view.frame.origin.y;
if (_way==yFoldViewTopToBottom)height=_view.bounds.size.height-(point.y-_view.frame.origin.y);
if (height+10>=_initialSizeView.height){
height=_initialSizeView.height;
opened=YES;
}
if (height-10<=0){
height=10;
closed=YES;
}
[self horizontalFoldWithHeight:height];
}
if (_way==yFoldViewRightToLeft || _way==yFoldViewLeftToRight){
float width=0;
if (_way==yFoldViewLeftToRight)width=point.x-_view.frame.origin.x;
if (_way==yFoldViewRightToLeft)width=_view.bounds.size.width-(point.x-_view.frame.origin.x);
if (width+10>=_initialSizeView.width){
width=_initialSizeView.width;
opened=YES;
}
if (width-10<=0){
width=10;
closed=YES;
}
[self verticalFoldWithWidth:width];
}
if (closed && _delegate && [_delegate respondsToSelector:@selector(yfoldview:view:isClose:)]){
[_delegate yfoldview:self view:_view isClose:YES];
}
if (opened && _delegate && [_delegate respondsToSelector:@selector(yfoldview:view:isOpen:)]){
[_delegate yfoldview:self view:_view isOpen:YES];
}
}
-(void) folded:(BOOL)folded{
[self initialize];
if (folded != _folded){
_folded=folded;
if (_folded){
for (int i=0;i<[_view.layer.sublayers count] ;i++){
[[_view.layer.sublayers objectAtIndex:i] setHidden:YES];
}
[_main3DLayer setHidden:NO];
NSLog(@"View folded");
}else{
for (int i=0;i<[_view.layer.sublayers count] ;i++){
[[_view.layer.sublayers objectAtIndex:i] setHidden:NO];
}
[_main3DLayer setHidden:YES];
NSLog(@"View Opened");
//[_main3DLayer removeFromSuperlayer];
}
}
[_view.layer setNeedsDisplay];
}
-(void) setPropertyToLayer:(CALayer*)layer withShadow:(CALayer*)shadow withHeight:(CGFloat)height ofIdx:(NSInteger)idx{
float heightPartCurr=height/_numberOfFolds;
float heightPartInitial=_initialSizeView.height/_numberOfFolds;
float percentOfHeight=(height/_initialSizeView.height);
shadow.opacity=1.0-percentOfHeight;
CGPoint posCurr=layer.position;
if (_way==yFoldViewBottomToTop && idx>0){
posCurr.y=heightPartCurr*idx;
if (layer.anchorPoint.y==1.0)posCurr.y+=heightPartCurr;
}
if (_way==yFoldViewTopToBottom && idx<([_layerFolds count]-1)){
posCurr.y=heightPartCurr*idx+(_initialSizeView.height-_currentSize.height);
if (layer.anchorPoint.y==1.0)posCurr.y+=heightPartCurr;
}
layer.position=posCurr;
float angle2=acos(heightPartCurr/heightPartInitial);
if (idx%2==0)angle2=angle2*-1;
CATransform3D transform=CATransform3DIdentity;
transform=CATransform3DRotate(transform, angle2, 1.0, 0.0, 0);
layer.transform=transform;
}
-(void) setPropertyToLayer:(CALayer*)layer withShadow:(CALayer*)shadow withWidth:(CGFloat)width ofIdx:(NSInteger)idx{
float widthPartCurr=width/_numberOfFolds;
float widthPartInitial=_initialSizeView.width/_numberOfFolds;
float percentOfwidth=(width/_initialSizeView.width);
shadow.opacity=1.0-percentOfwidth;
CGPoint posCurr=layer.position;
if (_way==yFoldViewLeftToRight && idx>0){
posCurr.x=widthPartCurr*idx;
if (layer.anchorPoint.x==1.0)posCurr.x+=widthPartCurr;
}
if (_way==yFoldViewRightToLeft && idx<([_layerFolds count]-1)){
posCurr.x=widthPartCurr*idx+(_initialSizeView.width-_currentSize.width);
if (layer.anchorPoint.x==1.0)posCurr.x+=widthPartCurr;
}
layer.position=posCurr;
float angle2=acos(widthPartCurr/widthPartInitial);
if (idx%2!=0)angle2=angle2*-1;
CATransform3D transform=CATransform3DIdentity;
transform=CATransform3DRotate(transform, angle2, 0.0, 1.0, 0);
layer.transform=transform;
}
-(void) animationStepForHeight{
[self horizontalFoldWithHeight:_currentSize.height andForce:YES];
_currentSize.height+=_increment;
if (_idxStep>_nbSteps){
[_timerForAnimation invalidate];
_timerForAnimation=nil;
if (_delegate && [_delegate respondsToSelector:@selector(yfoldview:view:animFinished:)]){
[_delegate yfoldview:self view:_view animFinished:YES];
}
}
_idxStep++;
}
-(void) animateHorizontalFoldFrom:(CGFloat)heightstart to:(CGFloat)heightEnd duration:(NSTimeInterval)duration{
float freq=1.0/10.0;
_nbSteps=(duration/freq)+1;
_increment=(heightEnd-heightstart)/_nbSteps;
_idxStep=0;
_currentSize.height=heightstart;
_timerForAnimation=[NSTimer scheduledTimerWithTimeInterval:freq
target:self
selector:@selector(animationStepForHeight)
userInfo:nil
repeats:YES];
[_timerForAnimation fire];
}
-(void) animationStepForWidth{
[self verticalFoldWithWidth:_currentSize.width andForce:YES];
_currentSize.width+=_increment;
if (_idxStep>_nbSteps){
[_timerForAnimation invalidate];
_timerForAnimation=nil;
if (_delegate && [_delegate respondsToSelector:@selector(yfoldview:view:animFinished:)]){
[_delegate yfoldview:self view:_view animFinished:YES];
}
}
_idxStep++;
}
-(void) animateVerticalFoldFrom:(CGFloat)widthstart to:(CGFloat)widthEnd duration:(NSTimeInterval)duration{
float freq=1.0/10.0;
_nbSteps=(duration/freq)+1;
_increment=(widthEnd-widthstart)/_nbSteps;
_idxStep=0;
_currentSize.width=widthstart;
_timerForAnimation=[NSTimer scheduledTimerWithTimeInterval:freq
target:self
selector:@selector(animationStepForWidth)
userInfo:nil
repeats:YES];
[_timerForAnimation fire];
}
-(void) reset{
[_main3DLayer removeFromSuperlayer];
[_main3DLayer release];
_main3DLayer=nil;
CGRect frm=_view.frame;
frm.size=_initialSizeView;
_currentSize=_initialSizeView;
_view.frame=frm;
[_layerFolds release];
_layerFolds=nil;
}
@end