我正在尝试构建自定义容器视图控制器并尝试创建自己的动画。我首先将点击的单元格移动到屏幕中心,然后通过缩放和显示新视图进行放大。我从这个开始:
- (void)showDetailViewController:(UIView *)viewToAnimate {
UIImageView *anImageView = [[UIImageView alloc] initWithImage:[self screenShot:_homeViewController.view]];
CGRect windowBounds = [[UIScreen mainScreen] bounds];
_detailViewController.view.frame = windowBounds;
[self addChildViewController:_detailViewController];
// get a screenshot of the old view before pushing the new view controller
_detailViewController.view.alpha = 0.0;
NSLog(@"center: %@", NSStringFromCGPoint(viewToAnimate.center));
[self transitionFromViewController:_homeViewController toViewController:_detailViewController duration:0.5 options:0 animations:^{
CGPoint windowCenter = CGPointMake(CGRectGetMidX(windowBounds), CGRectGetMidY(windowBounds));
viewToAnimate.center = windowCenter;
viewToAnimate.layer.transform = CATransform3DMakeScale(2, 2, 2);
NSLog(@"center 2 : %@", NSStringFromCGPoint(viewToAnimate.center));
_detailViewController.view.alpha = 1.0;
}completion:^(BOOL finished) {
[anImageView removeFromSuperview];
}];
[_detailViewController didMoveToParentViewController:self];
}
我不明白的是,如果我注释掉 _detailViewController.view.alpha = 0 和 _detailViewController.view.alpha = 1,我不会得到任何动画。它只是跳转到新的 childViewController。但是有了这两行,我确实看到了动画。这是为什么?谢谢。
编辑:这就是我设置容器控制器的方式:在 ContainerViewController 的 viewDidLoad 中:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
_homeViewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
_homeViewController.homeViewControllerDelegate = self;
_detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self setSubViewControllers:@[ _homeViewController, _detailViewController]];
_selectedViewController.view.frame = [[UIScreen mainScreen] bounds];
[self addChildViewController:self.selectedViewController];
[self.view addSubview:_selectedViewController.view];
[self didMoveToParentViewController:self];