4

I have a UITableView controller displaying static tableview. I have to update the project providing a secondary mode where this tableview is supposed to flip and to show different data. My approach would be to put all the static cells of both modes in one big static tableview, to perform a flip and to hide the cell which are not required at that specific moment. The flip should be performed only on the tableviewcontrollerview,so not affecting the navigationbar or the main tabbar. The code I've been trying to use so far for realising this simple magic trick is:

// Transition using a page flip.
    [UIView transitionFromView:self.tableview
                        toView:self.tableview
                      duration:0.7
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    completion:^(BOOL finished) {
                        if (finished) {
                           /* HIDE NOT REQUIRED CELL */

                        }
                }];

Unfortunately the result is a black screen. Any idea how to fix it?

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Claus
  • 5,662
  • 10
  • 77
  • 118

3 Answers3

8

Try using the below code instead:

[UIView transitionWithView:self.tableView 
                   duration:0.7
                   options:UIViewAnimationOptionTransitionFlipFromLeft 
                   animations:^{
                          /* any other animation you want */
                  } completion:^(BOOL finished) {
                          /* hide/show the required cells*/
                  }];
Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • I believe the issue here (which I just ran into without transitioning to the same view) is caused by other animations canceling or in some other way interfering with the transition. It's possible for the transition to become cancelled, leaving it in a state in which the completion block is NEVER CALLED. For me this resulted in the black screen because my completion block was manipulating my root view controller of the App to transition from the on-boarding experience to the main user experience. Hope this helps someone! – Beltalowda Jan 31 '15 at 03:05
1

I think the main problem is, that views can have max one superview. Which means a view can not be in two places in the same time. I would try capture the view as image and add it on its superview just before animation and remove the real tableview from its superview. And then you can animate between two different views (image view and table view). In completion block just remove the image view.

How to capture the view :How to capture current view screenshot and reuse in code? (iPhone SDK)

Community
  • 1
  • 1
Mert
  • 6,025
  • 3
  • 21
  • 33
  • this looks quite a ninja technique,but I will give a try. If you find the time for posting some sample code would be much appreciated ;) – Claus Feb 19 '13 at 16:08
1

I've accomplished this using a slightly different technique. Perhaps it's useful for you. the basic structure is that there is a MainContainerView and inside that is a MapView (in your case a table view). When I do the flip animation I grab the view of a separate view controller (list view) and add it as a subview to the MainContainerView and remove the MapView from the subview. When I want to flip back I do the opposite.

-(void)listViewButtonHandler:(id)sender
{
    if (_isMap) { //transition to view 2

        self.listViewController.view.frame = self.mainContainerView.bounds; //grab the view of a separate VC

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                               forView:self.mainContainerView
                                 cache:YES];
        [self.mapContainerView removeFromSuperview];
        [self.mainContainerView addSubview:self.listViewController.view];

        [UIView commitAnimations];
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"mapButton"] target:self action:@selector(listViewButtonHandler:)]; //change the button to another title

        _isMap = NO;
    } else {
        //transition to view 1
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                               forView:self.mainContainerView
                                 cache:YES];
        [self.listViewController.view removeFromSuperview];
        [self.mainContainerView addSubview:self.mapContainerView];

        [UIView commitAnimations];
        _isMap = YES;
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"actualListButton"] target:self action:@selector(listViewButtonHandler:)]; //change the button back

    }
}

I hope this is helpful and that it can apply to your situation!

sixstatesaway
  • 1,106
  • 1
  • 12
  • 25
  • Hey,thanks for your quick answer.Gonna try immediately, I'll let you know if it works... – Claus Feb 19 '13 at 16:01
  • The trick part adapting your code is that you have a main container view on which you apply the animation during which you remove the subview. I don't have a subview but just the tableview. Not sure if I'm clear – Claus Feb 19 '13 at 16:06
  • Right, you can restructure you code to have a `UIView` which is the `MainContainerView` and inside that put your `TableView`. In a separate `viewcontroller`you can have your second `TableView`??? – sixstatesaway Feb 19 '13 at 16:10