Skip to content

Commit

Permalink
Positions of view controllers can now be reset, multiple controllers …
Browse files Browse the repository at this point in the history
…can be inserted at the same time, and removed view controllers from matrix can now be animated or not.
  • Loading branch information
mmorey committed Jun 1, 2013
1 parent 48f6962 commit 1de3762
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 98 deletions.
6 changes: 5 additions & 1 deletion MSMatrixController/MSMatrixMasterViewController.h
Expand Up @@ -46,13 +46,17 @@ typedef enum {

- (id)initWithFrame:(CGRect)frame;

- (void)resetPositions:(NSArray *)viewControllers;

- (void)setControllers:(NSArray *)controllers;

- (void)insertControllers:(NSArray *)controllers shift:(MSDirection)direction;

- (void)insertController:(UIViewController *)controller shift:(MSDirection)direction;

- (void)removeController:(UIViewController *)controller;

- (void)removeController:(UIViewController *)controller shift:(MSDirection)direction;
- (void)removeController:(UIViewController *)controller shift:(MSDirection)direction animated:(BOOL)animated;

- (void)goToViewController:(UIViewController *)controller way:(MSPanWay)way animated:(BOOL)animated completion:(void (^)(void))completion;

Expand Down
234 changes: 140 additions & 94 deletions MSMatrixController/MSMatrixMasterViewController.m
Expand Up @@ -51,148 +51,194 @@ - (void)setControllers:(NSArray *)controllers

#pragma mark - Public methods

- (void)insertController:(UIViewController *)controller shift:(MSDirection)direction
- (void)insertControllers:(NSArray *)controllers shift:(MSDirection)direction
{
UIViewController *currentVisibleViewController = _visibleViewController;

NSMutableArray *controllers = [NSMutableArray arrayWithArray:_viewControllers];

if (controller.row < 0) {
controller.row = 0;
}

if (controller.col < 0) {
controller.col = 0;
}
NSMutableArray *currentControllers = [NSMutableArray arrayWithArray:_viewControllers];

switch (direction) {
case MSShiftHorizontal: {
for (UIViewController *child in controllers) {
if ((child.row == controller.row) && (child.col >= controller.col)) {
child.col += 1;
for (UIViewController *controller in controllers) {
if (controller.row < 0) {
controller.row = 0;
}

if (controller.col < 0) {
controller.col = 0;
}

switch (direction) {
case MSShiftHorizontal: {
for (UIViewController *child in currentControllers) {
if ((child.row == controller.row) && (child.col >= controller.col)) {
child.col += 1;
}
}
break;
}
break;
}
default: {
for (UIViewController *child in controllers) {
if ((child.col == controller.col) && (child.row >= controller.row)) {
child.row += 1;
default: {
for (UIViewController *child in currentControllers) {
if ((child.col == controller.col) && (child.row >= controller.row)) {
child.row += 1;
}
}
break;
}
break;
}

[currentControllers addObject:controller];
}

[controllers addObject:controller];
[self setControllers:controllers withFrame:[[UIScreen mainScreen] bounds]];
[self setControllers:currentControllers withFrame:[[UIScreen mainScreen] applicationFrame]];

_visibleViewController = currentVisibleViewController;
}

- (void)insertController:(UIViewController *)controller shift:(MSDirection)direction
{
[self insertControllers:@[controller] shift:direction];
}

- (void)resetPositions:(NSArray *)viewControllers {

UIViewController *currentVisibleViewController = _visibleViewController;
[self setControllers:viewControllers withFrame:[[UIScreen mainScreen] applicationFrame]];
_visibleViewController = currentVisibleViewController;

}

- (void)removeController:(UIViewController *)controller
{
[self removeControllers:@[controller]];
}

- (void)removeControllers:(NSArray *)controllers {
UIViewController *currentVisibleViewController = _visibleViewController;

NSMutableArray *controllers = [NSMutableArray arrayWithArray:_viewControllers];
[controllers removeObject:controller];
NSMutableArray *currentControllers = [NSMutableArray arrayWithArray:_viewControllers];
[currentControllers removeObjectsInArray:controllers];

[controller removeFromParentViewController];
[controller.view removeFromSuperview];
for (UIViewController *viewController in controllers) {
[viewController willMoveToParentViewController:nil];
[viewController.view removeFromSuperview];
[viewController removeFromParentViewController];
}

[self setControllers:controllers withFrame:[[UIScreen mainScreen] bounds]];
[self setControllers:currentControllers withFrame:[[UIScreen mainScreen] applicationFrame]];

_visibleViewController = currentVisibleViewController;
}

- (void)removeController:(UIViewController *)controller shift:(MSDirection)direction
- (void)removeController:(UIViewController *)controller shift:(MSDirection)direction animated:(BOOL)animated
{
UIViewController *rightViewController = controller.rightViewController;
UIViewController *leftViewController = controller.leftViewController;
UIViewController *topViewController = controller.topViewController;
UIViewController *bottomViewController = controller.bottomViewController;

NSMutableArray *controllers = [NSMutableArray arrayWithArray:_viewControllers];
[controllers removeObject:controller];

switch (direction) {
case MSShiftHorizontal: {

// No left or right view controller, just remove it
if (!leftViewController && !rightViewController) {
return;
}

for (UIViewController *child in controllers) {
if ((child.row == controller.row) && (child.col >= controller.col)) {
child.col -= 1;
[self removeController:controller];
} else {
// Adjacent view controllers exist, need to adjust their postion
NSMutableArray *controllers = [NSMutableArray arrayWithArray:_viewControllers];
[controllers removeObject:controller];

for (UIViewController *child in controllers) {
if ((child.row == controller.row) && (child.col >= controller.col)) {
child.col -= 1;
}
}
}

if (rightViewController) {
[self moveRightAnimated:YES withCompletion:^{

[controller removeFromParentViewController];

if (!animated) {
[controller willMoveToParentViewController:nil];
[controller.view removeFromSuperview];

[self setControllers:controllers withFrame:[[UIScreen mainScreen] bounds]];

CGRect newFrame = self.view.frame;
newFrame.origin.x += [[UIScreen mainScreen] bounds].size.width;
self.view.frame = newFrame;

_visibleViewController = rightViewController;
}];
} else {
[self moveLeftAnimated:YES withCompletion:^{

[controller removeFromParentViewController];
[controller.view removeFromSuperview];

[self setControllers:controllers withFrame:[[UIScreen mainScreen] bounds]];
[self resetPositions:controllers];
} else {

_visibleViewController = leftViewController;
}];
}
if (rightViewController) {
[self moveRightAnimated:YES withCompletion:^{

[controller willMoveToParentViewController:nil];
[controller.view removeFromSuperview];
[controller removeFromParentViewController];

[self setControllers:controllers withFrame:[[UIScreen mainScreen] applicationFrame]];
CGRect newFrame = self.view.frame;
newFrame.origin.x += [[UIScreen mainScreen] applicationFrame].size.width;
self.view.frame = newFrame;
_visibleViewController = rightViewController;
}];

} else {
[self moveLeftAnimated:YES withCompletion:^{

[controller willMoveToParentViewController:nil];
[controller.view removeFromSuperview];
[controller removeFromParentViewController];
[self setControllers:controllers withFrame:[[UIScreen mainScreen] applicationFrame]];
_visibleViewController = leftViewController;
}];

}
}
}
break;
}
case MSShiftVertical: {

// No top or bottom view controller, just remove it
if (!topViewController && !bottomViewController) {
return;
}

for (UIViewController *child in controllers) {
if ((child.col == controller.col) && (child.row >= controller.row)) {
child.row -= 1;
[self removeController:controller];
} else {
// Adjacent view controllers exist, need to adjust their postion
NSMutableArray *controllers = [NSMutableArray arrayWithArray:_viewControllers];
[controllers removeObject:controller];

for (UIViewController *child in controllers) {
if ((child.col == controller.col) && (child.row >= controller.row)) {
child.row -= 1;
}
}
}

if (bottomViewController) {
[self moveDownAnimated:YES withCompletion:^{

[controller removeFromParentViewController];

if (!animated) {
[controller willMoveToParentViewController:nil];
[controller.view removeFromSuperview];

[self setControllers:controllers withFrame:[[UIScreen mainScreen] bounds]];

CGRect newFrame = self.view.frame;
newFrame.origin.y += [[UIScreen mainScreen] bounds].size.height;
self.view.frame = newFrame;

_visibleViewController = bottomViewController;
}];
} else {
[self moveUpAnimated:YES withCompletion:^{

[controller removeFromParentViewController];
[controller.view removeFromSuperview];

[self setControllers:controllers withFrame:[[UIScreen mainScreen] bounds]];
[self resetPositions:controllers];
} else {
if (bottomViewController) {
[self moveDownAnimated:YES withCompletion:^{

[controller willMoveToParentViewController:nil];
[controller.view removeFromSuperview];
[controller removeFromParentViewController];

[self setControllers:controllers withFrame:[[UIScreen mainScreen] applicationFrame]];

CGRect newFrame = self.view.frame;
newFrame.origin.y += [[UIScreen mainScreen] applicationFrame].size.height;
self.view.frame = newFrame;

_visibleViewController = bottomViewController;
}];
} else {
[self moveUpAnimated:YES withCompletion:^{

[controller willMoveToParentViewController:nil];
[controller.view removeFromSuperview];
[controller removeFromParentViewController];
[self setControllers:controllers withFrame:[[UIScreen mainScreen] applicationFrame]];
_visibleViewController = topViewController;

}];
}
}

_visibleViewController = topViewController;
}];
}
}
break;
}
default:
Expand Down Expand Up @@ -263,7 +309,7 @@ - (void)moveController:(UIViewController *)controller toPosition:(Position)posit
controller.row = position.row;
controller.col = position.col;

[self setControllers:_viewControllers withFrame:[[UIScreen mainScreen] bounds]];
[self setControllers:_viewControllers withFrame:[[UIScreen mainScreen] applicationFrame]];

_visibleViewController = currentVisibleViewController;
}
Expand Down
2 changes: 0 additions & 2 deletions MSMatrixControllerDemo.xcodeproj/project.pbxproj
Expand Up @@ -29,7 +29,6 @@
A9F3903917075F4300C709D0 /* Arrow-Right@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = A9F3903117075F4300C709D0 /* Arrow-Right@2x.png */; };
A9F3903A17075F4300C709D0 /* Arrow-Up.png in Resources */ = {isa = PBXBuildFile; fileRef = A9F3903217075F4300C709D0 /* Arrow-Up.png */; };
A9F3903B17075F4300C709D0 /* Arrow-Up@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = A9F3903317075F4300C709D0 /* Arrow-Up@2x.png */; };
A9FD76F0170716470005AB67 /* MSMatrixControllerDemo-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = A97A5FD9170600640009A57F /* MSMatrixControllerDemo-Info.plist */; };
A9FD76F51707394B0005AB67 /* MSDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9FD76F41707394B0005AB67 /* MSDemoViewController.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -228,7 +227,6 @@
A97A5FE0170600640009A57F /* Default@2x.png in Resources */,
A97A5FE2170600640009A57F /* Default-568h@2x.png in Resources */,
A97A5FE917060B7B0009A57F /* iPhone.storyboard in Resources */,
A9FD76F0170716470005AB67 /* MSMatrixControllerDemo-Info.plist in Resources */,
A9F3903417075F4300C709D0 /* Arrow-Down.png in Resources */,
A9F3903517075F4300C709D0 /* Arrow-Down@2x.png in Resources */,
A9F3903617075F4300C709D0 /* Arrow-Left.png in Resources */,
Expand Down
2 changes: 1 addition & 1 deletion MSMatrixControllerDemo/MSDemoViewController.m
Expand Up @@ -94,7 +94,7 @@ - (IBAction)insertDown:(id)sender

- (IBAction)removeTapped:(id)sender
{
[self.matrixViewController removeController:self shift:MSShiftHorizontal];
[self.matrixViewController removeController:self shift:MSShiftHorizontal animated:YES];
}

- (void)updateDisplay
Expand Down

0 comments on commit 1de3762

Please sign in to comment.