How to switch views with animation - objective c
Asked Answered
R

3

6

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.

Rumple answered 21/4, 2012 at 7:37 Comment(0)
R
10

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

Rumple answered 21/4, 2012 at 9:10 Comment(3)
If this is how you solved your problem, mark it as the correct answer.Bludgeon
How? when I press the "accept answer check mark" it say I can accept my own answer in two daysRumple
Alright, sorry I forgot about that limitation. Just making sure!Bludgeon
C
4

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];
}
Chitin answered 21/4, 2012 at 7:42 Comment(2)
thanks the problem is that my toolbar is also animating, I think because he is also a subclass of the viewRumple
@Rumple you most likely mean that it is a subview and not a subclass, correct?Aa
V
1

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;
    }
}
Vey answered 21/4, 2012 at 7:42 Comment(1)
thanks the problem is that my toolbar is also animating, I think because he is also a subclass of the viewRumple

© 2022 - 2024 — McMap. All rights reserved.