iosobjective-cuiviewanimationchildviewcontrollerparentviewcontroller

How to show animation to a child view as it is appears in a UIViewController


I Have one View COntroller. on which i have a segment control. and a containerView. in that container view i am showing tableviewcontroller. as a child view.

I have a segment control. And 2 UITableViewControllers.Which are child views to the view controller on which they are plotted. By default first one will be selected. When we tap on the second one. It should come with some animation e.g. sliding in. I am attaching my code. but its not showing any animation. Can anybody tell me where am I making mistake?

PS : I am new to ios dev. And this is a part of my demo project.

`

- (IBAction)segmentChanged:(UISegmentedControl *)sender
{
    FirstTableTableViewController * ftc = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstTableTableViewController"];

    SecondTableViewController * stc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondTableTableViewController"];


    if (sender.selectedSegmentIndex == 0)
    {

        ftc.dataArray =[[NSMutableArray alloc]initWithArray:tab1];
        [self pushViewControllers:ftc :stc];
    }
    else
    {
        stc.dataArray =[[NSMutableArray alloc]initWithArray:tab2];
        [self pushViewControllers:stc :ftc];
    }
}

- (void)hideContentController: (UITableViewController *) content
{
    [content willMoveToParentViewController:nil]; // 1
    [content.view removeFromSuperview]; // 2
    [content removeFromParentViewController]; // 3
}

-(void)displayContentController:(UITableViewController *) content;
{
    [self addChildViewController:content]; // 1
    content.view.frame = self.contentView.bounds; // 2
    [self.contentView addSubview:content.view];
    [content didMoveToParentViewController:self]; // 3
}

-(void)pushViewControllers:(UITableViewController *)newVC :(UITableViewController *)oldVC
{
    [self hideContentController:oldVC];
    [newVC.view layoutIfNeeded];

    CGRect containerFrame = self.contentView.frame;
    CGFloat pt = CGRectGetMaxX(self.contentView.frame);

    CGRect otherOffsetRect = CGRectMake(pt, self.contentView.frame.origin.y, self.contentView.frame.size.width, self.contentView.frame.size.height);

    newVC.view.frame = otherOffsetRect;
    [UIView animateWithDuration:1.0f
                          delay:0.5f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         newVC.view.frame = containerFrame;
                     }
                     completion:^(BOOL finished)
                    {
                         [self addChildViewController:newVC]; // 1
                         [self.contentView addSubview:newVC.view];
                         [newVC didMoveToParentViewController:self]; // 3

                     }];
}

`


Solution

  •      FirstTableTableViewController * ftc = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstTableTableViewController"];
    
        SecondTableViewController * stc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondTableTableViewController"];
    
    
        if (sender.selectedSegmentIndex == 0)
        {
    
            ftc.dataArray =[[NSMutableArray alloc]initWithArray:tab1];
            [self pushViewControllers:ftc :stc];
        }
        else
        {
            stc.dataArray =[[NSMutableArray alloc]initWithArray:tab2];
            [self pushViewControllers:stc :ftc];
        }
    }
    
    - (void)hideContentController: (UITableViewController *) content
    {
        [content willMoveToParentViewController:nil]; // 1
        [content.view removeFromSuperview]; // 2
        [content removeFromParentViewController]; // 3
    }
    
    -(void)displayContentController:(UITableViewController *) content;
    {
        [self addChildViewController:content]; // 1
        content.view.frame = self.contentView.bounds; // 2
        [self.contentView addSubview:content.view];
        [content didMoveToParentViewController:self]; // 3
    }
    
    -(void)pushViewControllers:(UITableViewController *)newVC :(UITableViewController *)oldVC
    {
        CGRect ogFrame = self.contentView.bounds;
        [self hideContentController:oldVC];
    
        [self displayContentController:newVC];
        CGFloat pt = CGRectGetWidth(oldVC.view.frame);
    
        CGRect newFrame = self.contentView.bounds;
    
        newFrame.origin.x += pt;
    
        newFrame.size.height = self.contentView.frame.size.height;
        newFrame.size.width = self.contentView.frame.size.width;
    
        newVC.view.frame = newFrame;
        [UIView animateWithDuration:0.6f
                              delay:0.5f
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             newVC.view.frame = ogFrame;
                         }
                         completion:^(BOOL finished)
                        {}];
    }
    

    This one Solved it.

    Although I am sure there can be a more efficient way to solve this. Please share ways to make this code more efficient.