(更新)Container View Controller
这段时间在想如何通过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:
-
It calls the container’s
addChildViewController:
method to add the child. Calling theaddChildViewController:
method also calls the child’swillMoveToParentViewController:
method automatically. -
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. -
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:
-
Calls the child’s
willMoveToParentViewController:
method with a parameter ofnil
to tell the child that it is being removed. -
Cleans up the view hierarchy.
-
Calls the child’s
removeFromParentViewController
method to remove it from the container. Calling theremoveFromParentViewController
method automatically calls the child’sdidMoveToParentViewController:
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:
-
Starts both view controller transitions.
-
Calculates two new frame positions used to perform the transition animation.
-
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. -
The animation step to perform to get the views swapped.
-
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给出了解答,利用方法
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中。