(更新)Container View Controller

本文介绍了一种使用ContainerViewController在iOS应用中实现视图切换的方法,包括添加和移除子视图控制器的过程,以及如何通过动画过渡平滑地切换视图。此外,还提到了利用UIScrollView和UIPageControl实现类似iPhone主屏幕的图标滑动切换效果。

这段时间在想如何通过segmentedControl来切换view,搜到好多办法,大多都是直接addSubview好几个view,然后在segmentValueChanged中对不需要显示的view进行隐藏

[view setHidden:YES];

或者是直接从superView删除

[view removeFromSuperView];

最后终于找到也许是最官方的做法,利用Container View Controller。

Implementing a Custom Container View Controller

Once you’ve designed your class’s behavior and determined many aspects of its public API, you are ready to start implementing the container. The goal of implementing a container is to be able to add another view controller’s view (and associated view hierarchy) as a subtree in your container’s view hierarchy. The child remains responsible for its own view hierarchy, save for where the container decides to place it onscreen. When you add the child’s view, you need to ensure that events continue to be distributed to both view controllers. You do this by explicitly associating the new view controller as a child of the container.

The UIViewController class provides methods that a container view controller uses to manage the relationship between itself and its children. The complete list of methods and properties is in the reference; see “Managing Child View Controllers in a Custom Container” inUIViewController Class Reference

Important: These UIViewController methods are only intended to be used to implement container view controllers; do not call them in a content view controller.

 

Adding and Removing a Child

Listing 14-1 shows a typical implementation that adds a view controller as a child of another view controller. Each numbered step in the listing is described in more detail following the listing.

Listing 14-1  Adding another view controller’s view to the container’s view hierarchy

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

Here’s what the code does:

  1. It calls the container’s addChildViewController: method to add the child. Calling the addChildViewController: method also calls the child’s willMoveToParentViewController: method automatically.

  2. It accesses the child’s view property to retrieve the view and adds it to its own view hierarchy. The container sets the child’s size and position before adding the view; containers always choose where the child’s content appears. Although this example does this by explicitly setting the frame, you could also use layout constraints to determine the view’s position.

  3. It explicitly calls the child’s didMoveToParentViewController: method to signal that the operation is complete.

Eventually, you want to be able to remove the child’s view from the view hierarchy. In this case, shown in Listing 14-2, you perform the steps in reverse.

Listing 14-2  Removing another view controller’s view to the container’s view hierarchy

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

Here’s what this code does:

  1. Calls the child’s willMoveToParentViewController: method with a parameter of nil to tell the child that it is being removed.

  2. Cleans up the view hierarchy.

  3. Calls the child’s removeFromParentViewController method to remove it from the container. Calling theremoveFromParentViewController method automatically calls the child’s didMoveToParentViewController: method.

For a container with essentially static content, adding and removing view controllers is as simple as that. Whenever you want to add a new view, add the new view controller as a child first. After the view is removed, remove the child from the container. However, sometimes you want to animate a new child onto the screen while simultaneously removing another child. Listing 14-3 shows an example of how to do this.

Listing 14-3  Transitioning between two view controllers

- (void) cycleFromViewController: (UIViewController*) oldC
            toViewController: (UIViewController*) newC
{
    [oldC willMoveToParentViewController:nil];                        // 1
    [self addChildViewController:newC];
 
    newC.view.frame = [self newViewStartFrame];                       // 2
    CGRect endFrame = [self oldViewEndFrame];
 
    [self transitionFromViewController: oldC toViewController: newC   // 3
          duration: 0.25 options:0
          animations:^{
             newC.view.frame = oldC.view.frame;                       // 4
             oldC.view.frame = endFrame;
           }
           completion:^(BOOL finished) {
             [oldC removeFromParentViewController];                   // 5
             [newC didMoveToParentViewController:self];
            }];
}

Here’s what this code does:

  1. Starts both view controller transitions.

  2. Calculates two new frame positions used to perform the transition animation.

  3. Calls the transitionFromViewController:toViewController:duration:options:animations:completion: method to perform the swap. This method automatically adds the new view, performs the animation, and then removes the old view.

  4. The animation step to perform to get the views swapped.

  5. When the transition completes, the view hierarchy is in its final state, so it finishes the operation by sending the final two notifications.

最后的14-3给出了解答,利用方法

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Parameters
fromViewController

A view controller whose view is currently visible in the parent’s view hierarchy.

toViewController

A child view controller whose view is not currently in the view hierarchy.

duration

The total duration of the animations, in seconds. If you pass zero, the changes are made without animating them.

options

A mask of options indicating how you want to perform the animations. For a list of valid constants, see UIViewAnimationOptions.

animations

A block object containing the changes to commit to the views. Here you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.

completion

A block to be called when the animation completes.

The block takes the following parameters:

finished

YES if the animation finished; NO if it was skipped.

Discussion

This method adds the second view controller’s view to the view hierarchy and then performs the animations defined in your animations block. After the animation completes, it removes the first view controller’s view from the view hierarchy.

This method is only intended to be called by an implementation of a custom container view controller. If you override this method, you must callsuper in your implementation.

最后我的解决方案是:

 1 - (IBAction)didSegmentControlValueChange:(UISegmentedControl *)sender
 2 {
 3     NSInteger currentSelectedSegmentIndex = sender.selectedSegmentIndex;
 4     UIViewController *newVC = [self viewControllerForSegmentIndex:currentSelectedSegmentIndex];
 5     [self.currentViewController willMoveToParentViewController:nil];
 6     [self addChildViewController:newVC];
 7     //以下是页面左右滑动切换效果的实现
 8     CGRect rect = CGRectMake(0, SEGMENT_HEIGHT, self.view.frame.size.width, self.view.frame.size.height - SEGMENT_HEIGHT);
 9     CGPoint leftCenter = CGPointMake(rect.origin.x - rect.size.width / 2, rect.origin.y + rect.size.height / 2);
10     CGPoint rightCenter = CGPointMake(rect.size.width * 3 / 2, rect.origin.y + rect.size.height / 2);
11     CGPoint middleCenter = CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2);
12     
13     newVC.view.frame = rect;
14     newVC.view.center = currentSelectedSegmentIndex > _lastSelectedSegmentIndex ? rightCenter :leftCenter;
15 //    newVC.view.alpha = 0;
16     [self transitionFromViewController:self.currentViewController toViewController:newVC duration:0.25 options:0 animations:^{
17         //这里添加改变view属性的代码来产生动画
18         newVC.view.center = middleCenter;
19 //        newVC.view.alpha = 1;
20 //        self.currentViewController.view.alpha = 0;
21         self.currentViewController.view.center = currentSelectedSegmentIndex > _lastSelectedSegmentIndex ? leftCenter : rightCenter;
22     } completion:^(BOOL finished) {
23         [self.currentViewController removeFromParentViewController];
24         [newVC didMoveToParentViewController:self];
25         self.currentViewController = newVC;
26         self.lastSelectedSegmentIndex = currentSelectedSegmentIndex;
27     }];    
28 }
29 
30 - (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
31     UIViewController *vc;
32     switch (index) {
33         case 1:
34             vc = [self.storyboard instantiateViewControllerWithIdentifier:@"first table view"];
35             break;
36         case 0:
37             vc = [self.storyboard instantiateViewControllerWithIdentifier:@"second table view"];
38             break;
39         case 2:
40             vc = [self.storyboard instantiateViewControllerWithIdentifier:@"third table view"];
41         default:
42             break;
43     }
44     return vc;
45 }

 更新:

最近又发现,利用page control也能达到相应的目的,做到像iphone 的 home界面不同屏图标滑动切换的那样。

page control核心就是利用一个只能横向滑动的scroll view,只需要设置content size即可,然后将不同的view放在固定的content offset中。

转载于:https://www.cnblogs.com/mystory/archive/2012/12/18/2823972.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值