Eyal
Eyal

Reputation: 10818

How to switch views with animation - objective c

I have a viewController that has a tableView and a mapView, and only one is visible. I also have a toolbar with segment control with two buttons (list and map)

How do I switch between the table view and the map view ? and it's important that the toolbar will stay locked without animating with the views.

Upvotes: 6

Views: 6195

Answers (3)

Eyal
Eyal

Reputation: 10818

After more thinking I found a solution, add another view as a container view for both table view and map view.
This way i can do:

   [UIView transitionWithView:self.someContainerView
                     duration:1.0
                     options:UIViewAnimationOptionTransitionFlipFromLeft 
                     animations:^{
                         self.mapView.hidden   = !showingMapView;
                         self.tableView.hidden = showingMapView;
                     } completion:nil
    ];  

without flipping the toolbar

Upvotes: 10

Dinesh
Dinesh

Reputation: 6532

Try below code for show table and mapview:

Hide the mapview and tableview in segmentedControlIndexChanged:

- (IBAction)segmentedControlIndexChanged {
    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0: //it's show tableview
            [UIView transitionWithView: self.view
                              duration:1.0 
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            animations:^{
                                self.mapView.hidden   = YES;
                                self.tableView.hidden = NO; }
                            completion:nil];

              break;

        case 1: //it's show mapview
            [UIView transitionWithView:self.view
                              duration:1.0
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            animations:^{
                                self.mapView.hidden   = NO;
                                self.tableView.hidden = YES; }
                            completion:nil];
            break;

        default:
              break;
    }
}

Upvotes: 1

Jacob Relkin
Jacob Relkin

Reputation: 163228

You can use a UIView animation transition, passing the transitioning views' superview:

- (IBAction)segmentIndexChanged {
   BOOL showingMapView = (BOOL)self.segmentedControl.selectedSegmentIndex;
   [UIView transitionWithView:self.view
                     duration:1.0
                      options:UIViewAnimationOptionTransitionFlipFromLeft 
                   animations:^{
     self.mapView.hidden   = !showingMapView;
     self.tableView.hidden = showingMapView;
   } completion:nil];
}

Upvotes: 4

Related Questions