-
Notifications
You must be signed in to change notification settings - Fork 9
/
L0FlipViewController.m
137 lines (100 loc) · 3.51 KB
/
L0FlipViewController.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
//
// L0FlipViewController.m
// L0FlipViewController
//
// Created by ∞ on 19/02/09.
// Copyright 2009 Emanuele Vulcano. All rights reserved.
//
#if TARGET_OS_IPHONE
#import "L0FlipViewController.h"
@interface L0FlipViewController ()
- (void) _setFlippedWithoutChecking:(BOOL) flipped animated:(BOOL) animated;
@end
@implementation L0FlipViewController
@synthesize frontController, backController;
@synthesize cacheViewsDuringFlip;
- (id) initWithFrontController:(UIViewController*) front backController:(UIViewController*) back;
{
if (self = [self initWithNibName:nil bundle:nil]) {
NSAssert(front && back, @"Both front and back must be set.");
self.frontController = front;
self.backController = back;
self.cacheViewsDuringFlip = YES;
}
return self;
}
- (void) dealloc;
{
[frontController release];
[backController release];
[super dealloc];
}
- (UIViewController*) hiddenController;
{
return isFlipped? self.frontController : self.backController;
}
- (UIViewController*) currentController;
{
return !isFlipped? self.frontController : self.backController;
}
- (void) setFlipped:(BOOL) flipped animated:(BOOL) animated;
{
if (isFlipped == flipped) return;
[self _setFlippedWithoutChecking:flipped animated:animated];
}
- (void) _setFlippedWithoutChecking:(BOOL) flipped animated:(BOOL) animated;
{
UIViewController* next = flipped? self.hiddenController : self.frontController;
UIViewController* current = self.currentController;
if (current == next) current = nil;
[current viewWillDisappear:animated];
BOOL animationsWereEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
UIView* nextView = next.view; // this loads it.
nextView.frame = self.view.bounds;
nextView.hidden = YES;
[self.view addSubview:nextView];
[UIView setAnimationsEnabled:animationsWereEnabled];
[next viewWillAppear:animated];
if (animated) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationTransition:(flipped ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:self.cacheViewsDuringFlip];
[UIView setAnimationDuration:1.0];
}
for (UIView* v in self.view.subviews)
[v removeFromSuperview];
[self.view addSubview:nextView];
nextView.hidden = NO;
if (animated)
[UIView commitAnimations];
[current viewDidDisappear:animated];
[next viewDidAppear:animated];
isFlipped = flipped;
}
- (BOOL) isFlipped { return isFlipped; }
- (void) setFlipped:(BOOL) flipped { [self setFlipped:flipped animated:NO]; }
- (void) showFront { [self setFlipped:NO animated:YES]; }
- (void) showBack { [self setFlipped:YES animated:YES]; }
- (void) loadView;
{
if (self.nibName)
[self.nibBundle loadNibNamed:self.nibName owner:self options:nil];
NSAssert(self.frontController && self.backController, @"Both front and back must be set.");
UIView* view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
self.view = view;
[self _setFlippedWithoutChecking:NO animated:NO];
[self viewDidLoad];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation;
{
return [self.frontController shouldAutorotateToInterfaceOrientation:interfaceOrientation] && [self.backController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (void) didReceiveMemoryWarning;
{
[super didReceiveMemoryWarning];
// this ensures the current controller's view is destroyed
// along with ours, if ours was.
[self.currentController didReceiveMemoryWarning];
}
@end
#endif