我正在尝试组合一个简单的容器视图控制器,它允许我从任何方向呈现子视图控制器(从右侧像标准导航控制器一样,也从顶部、底部和左侧放置。
为 iOS 5 开发时,我使用父/子视图控制器关系并使用带有自定义动画块的 transitionFromViewController 在视图中滑动。
所有消息(viewWillAppear 等)都正确传递给子视图控制器,但问题是例如viewWillAppear
似乎是从动画循环内部调用的(即,当我设置动画属性时,如 backgroundColor 或子视图框架- 子视图控制器的视图,它们实际上是动画的)。我不想要这种行为,因为我想在显示视图控制器之前使用 viewWillAppear 方法来执行初始化。我不希望这段代码被动画化。
这是我的代码的简化版本:
@implementation ContainerViewController
@synthesize currentViewController;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.currentViewController = [[SimpleViewController alloc] init];
[self addChildViewController:self.currentViewController];
self.currentViewController.view.frame = self.view.bounds;
[self.view addSubview:self.currentViewController.view];
}
- (void) slideViewController {
UIViewController* previousViewController = self.currentViewController;
UIViewController* viewController = [[SimpleViewController alloc] init];
viewController.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0);
[self addChildViewController:viewController];
[previousViewController willMoveToParentViewController:nil];
[self transitionFromViewController:previousViewController toViewController:viewController duration:1.0 options:UIViewAnimationOptionTransitionNone animations:^{
viewController.view.frame = self.view.bounds;
} completion:^(BOOL finished) {
[previousViewController removeFromParentViewController];
[viewController didMoveToParentViewController:self];
}];
self.currentViewController = viewController;
}
- (void) loop {
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self slideViewController];
[self loop];
});
}
- (void)viewDidAppear:(BOOL)animated {
[self loop];
}
@end
@implementation SimpleViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewWillAppear:(BOOL)animated {
CGFloat red = (arc4random()%256)/255.0;
CGFloat green = (arc4random()%256)/255.0;
CGFloat blue = (arc4random()%256)/255.0;
self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
}
@end
如果您尝试运行此代码,您将看到一个新的视图控制器从右侧滑出。问题是该backgroundColor
视图控制器的视图从白色变为随机颜色(参见viewWillAppear
方法SimpleViewController
)。我希望backgroundColor
立即设置视图而不设置动画。