我正在实现我自己的导航 Controller ,我正在伪造添加新 View 的动画,就像在 UINavigationContoller 中一样。
下面的代码效果很好,但是如果我移动 -addSubview:
,在 UIView 动画调用之前,它会在 ios7 中设置动画,但在 ios6 中不会,问题是为什么? (我的意思是,为什么它会有所不同,因为动画将被异步安排和执行,对吗?或者我可能认为它会以某种方式影响动画的开始状态?)
- (void)didAddViewControllerInNavigationStack:(NSArray*)navigationStack
{
if (self.activeViewController) {
[self.activeViewController viewWillDisappear:YES];
[self.activeViewController viewDidDisappear:YES];
}
self.activeViewController = navigationStack.firstObject;
if (!self.activeViewController) return;
CGRect windowRect = [[UIScreen mainScreen] bounds];
windowRect.origin.x = windowRect.size.width;
self.activeViewController.view.frame = windowRect;
[self.activeViewController viewWillAppear:YES];
[UIView transitionWithView:self.view
duration:0.32
options:UIViewAnimationOptionCurveEaseOut
animations:^ { self.activeViewController.view.frame = [[UIScreen mainScreen] bounds]; }
completion:^(BOOL isDone){[self.activeViewController viewDidAppear:YES];}];
[self.view addSubview:self.activeViewController.view];
[UIView commitAnimations];
}
từ transitionWithView:duration:options:animations:completion:
的文档中,据说您应该在动画 block 中添加 subview :
The block you specify in the animations parameter contains whatever state changes you want to make. You can use this block to add, remove, show, or hide subviews of the specified view.
此外,您在调用 commitAnimations
时并未调用 beginAnimations:context:
。你在这里混合东西。基于 block 的动画 API 不需要 begin-end API。
您的代码应该是:
[UIView transitionWithView:self.view
duration:0.32
options:UIViewAnimationOptionCurveEaseOut
animations:^ { [self.view addSubview:self.activeViewController.view]; self.activeViewController.view.frame = [[UIScreen mainScreen] bounds]; }
completion:^(BOOL isDone){[self.activeViewController viewDidAppear:YES];}];
Tôi là một lập trình viên xuất sắc, rất giỏi!