1

我有一个视图控制器。我有一个段控制。和一个容器视图。在那个容器视图中,我正在显示 tableviewcontroller。作为子视图。

我有一个段控制。还有 2 个 UITableViewControllers。它们是绘制它们的视图控制器的子视图。默认情况下,将选择第一个。当我们点击第二个。它应该带有一些动画,例如滑入。我正在附上我的代码。但它没有显示任何动画。谁能告诉我我在哪里犯错?

PS:我是ios开发新手。这是我的演示项目的一部分。

`

- (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

                     }];
}

`

4

1 回答 1

1
     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)
                    {}];
}

这个解决了。

虽然我确信可以有更有效的方法来解决这个问题。请分享使此代码更高效的方法。

于 2015-07-24T11:31:48.477 回答