gpt4 book ai didi

ios - 如何向 subview 显示动画,因为它出现在 UIViewController 中

转载 作者:行者123 更新时间:2023-11-29 12:17:39 25 4
gpt4 key购买 nike

我有一个 View Controller 。我有一个段控制。和一个容器 View 。在那个容器 View 中,我正在显示 tableviewcontroller。作为 subview 。

我有一个段控件。和 2 个 UITableViewControllers。它们是绘制它们的 View Controller 的 subview 。默认情况下将选择第一个。当我们点击第二个时。它应该带有一些动画,例如滑进去。我附上了我的代码。但它没有显示任何动画。谁能告诉我哪里出错了?

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

}];
}

`

最佳答案

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

这个解决了。

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

关于ios - 如何向 subview 显示动画,因为它出现在 UIViewController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31607805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com