EnkiUtils
Small library of classes that Enki Labs uses internally.
 All Classes Functions Properties Pages
EnkiUnorderedController.m
1 //
2 // EnkiUnorderedController.m
3 //
4 // Created by Paul Cezanne on 4/2/13.
5 // Copyright (c) 2013 Enki Labs. All rights reserved.
6 //
7 
8 #import "EnkiUnorderedController.h"
9 
11 - (void) adjustTarget:(UIViewController *) vc withDirection:(slideDirection) direction;
12 - (void) slideCurrent:(UIViewController *) vc withDirection:(slideDirection) direction;
13 - (void) slideTarget:(UIViewController *) vc withDirection:(slideDirection) direction;
14 
15 - (void) replaceViewController:(UIViewController *) target withDirection:(slideDirection)direction withCover:(BOOL) coverIt;
16 
17 @end
18 
19 @implementation EnkiUnorderedController
20 
21 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
22 {
23 
24  self = [super initWithNibName:nil bundle:nibBundleOrNil];
25  if (self) {
26  // Custom initialization
27  }
28  return self;
29 }
30 
31 - (void)initCurrentWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
32 {
33  Class vcClass = NSClassFromString (nibNameOrNil);
34  self.current = [[vcClass alloc] initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
35 
36  if (self.current) {
37 
38  self.current.view.frame = self.view.frame;
39 
40  [self addChildViewController:self.current];
41  [self.current didMoveToParentViewController:self];
42  [self.view addSubview: self.current.view];
43 
44  }
45 }
46 
47 
48 - (void)viewDidLoad
49 {
50  [super viewDidLoad];
51  // Do any additional setup after loading the view.
52 }
53 
54 #pragma mark - Category methods
55 
56 
57 - (void) replaceViewController:(UIViewController *) target withDirection:(slideDirection) direction;
58 {
59  [self replaceViewController:target withDirection:direction withCover:NO];
60 }
61 
62 - (void) coverViewController:(UIViewController *) target withDirection:(slideDirection) direction;
63 {
64  [self replaceViewController:target withDirection:direction withCover:YES];
65 }
66 
67 - (void) uncoverViewControllerwithDirection:(slideDirection) direction;
68 {
69  // knock the current off the screen to start with
70  [self adjustTarget:_current withDirection:direction];
71 
72  // slide the current one away// slide the target in[UIView animateWithDuration:0.5
73  delay:0
74  options: UIViewAnimationOptionCurveEaseInOut
75  animations:^{
76 
77  [self slideCurrent:_cover withDirection:direction];
78 
79 
80  [self slideTarget:_current withDirection:direction];
81  }
82  completion:^(BOOL finished){
83  [_cover removeFromParentViewController];
84  _cover = nil;
85  }];
86 }
87 
88 #pragma mark - Extension methods
89 
91 - (void) replaceViewController:(UIViewController *) target withDirection:(slideDirection)direction withCover:(BOOL) coverIt;
92 {
93  target.view.frame = self.view.frame;
94 
95  // add the target as a child view controller.
96  [self addChildViewController:target];
97  [target didMoveToParentViewController:self];
98  [self.view addSubview: target.view];
99 
100  // knock the target off the screen to start with
101  [self adjustTarget:target withDirection:direction];
102 
103  // slide the current one away// slide the target in[UIView animateWithDuration:0.5
104  delay:0
105  options: UIViewAnimationOptionCurveEaseInOut
106  animations:^{
107 
108  [self slideCurrent:_current withDirection:direction];
109 
110 
111  [self slideTarget:target withDirection:direction];
112  }
113  completion:^(BOOL finished){
114  if (coverIt) {
115  if (_cover != nil) {
116  [_cover removeFromParentViewController];
117  }
118  _cover = target;
119  }else {
120  [_current removeFromParentViewController];
121  _current = target;
122  }
123  }];
124 }
125 
127 - (void) slideCurrent:(UIViewController *) vc withDirection:(slideDirection) direction;
128 {
129  CGRect currentFrame = vc.view.frame;
130  switch (direction) {
131  case kSlideUp:
132  currentFrame.origin.y -= currentFrame.size.height;
133  break;
134  case kSlideDown:
135  currentFrame.origin.y += currentFrame.size.height;
136  break;
137  case kSlideLeft:
138  currentFrame.origin.x -= currentFrame.size.width;
139  break;
140  case kSlideRight:
141  currentFrame.origin.x += currentFrame.size.width;
142  break;
143 
144  default:
145  break;
146  }
147  vc.view.frame = currentFrame;
148 
149 }
150 
152 - (void) slideTarget:(UIViewController *) vc withDirection:(slideDirection) direction;
153 {
154  CGRect targetFrame = vc.view.frame;
155  switch (direction) {
156  case kSlideUp:
157  targetFrame.origin.y = 0;
158  break;
159  case kSlideDown:
160  targetFrame.origin.y = 0;
161  break;
162  case kSlideLeft:
163  targetFrame.origin.x = 0;
164  break;
165  case kSlideRight:
166  targetFrame.origin.x = 0;
167  break;
168 
169  default:
170  break;
171  }
172  vc.view.frame = targetFrame;
173 
174 }
175 
176 
178 - (void) adjustTarget:(UIViewController *) vc withDirection:(slideDirection) direction;
179 {
180  CGRect targetFrame = vc.view.frame;
181 
182  switch (direction) {
183  case kSlideUp:
184  targetFrame.origin.y += targetFrame.size.height;
185  break;
186  case kSlideDown:
187  targetFrame.origin.y -= targetFrame.size.height;
188  break;
189  case kSlideLeft:
190  targetFrame.origin.x += targetFrame.size.width;
191  break;
192  case kSlideRight:
193  targetFrame.origin.x -= targetFrame.size.width;
194  break;
195 
196  default:
197  break;
198  }
199  vc.view.frame = targetFrame;
200 
201 }
202 
203 - (void)didReceiveMemoryWarning
204 {
205  [super didReceiveMemoryWarning];
206  // Dispose of any resources that can be recreated.
207 }
208 
209 @end