0

我使用与以下非常相似的代码:

- (void)flipToViewController:(UIViewController*)targetViewController
              transition:(UIViewAnimationTransition)transition

{ if( targetViewController ) { [[[self activeViewController] view] setUserInteractionEnabled:NO];

  // force the view to be instantiated (loadView/layoutSubviews)
  [[targetViewController view] setHidden:NO];

  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.8f];
  [UIView setAnimationTransition:transition forView:self.view cache:YES];

  [targetViewController viewWillAppear:YES];
  [[self activeViewController] viewWillDisappear:YES];

  [[[self activeViewController] view] removeFromSuperview];

  [[self view] addSubview:[targetViewController view]];

  [[self activeViewController] viewDidDisappear:YES];
  [targetViewController viewDidAppear:YES];

  [[targetViewController view] setUserInteractionEnabled:YES];

  [UIView commitAnimations];

  [self setActiveViewController:targetViewController];
  }

}

我使用翻转过渡,但是,当我的 targetViewController 翻转到视图中时,视图未正确初始化。UISegmentedControl 看起来很奇怪,并且一个子视图位于错误的位置。

只有在动画完成后,所有内容才会进入正确的位置。

我在 viewWillAppear(不是 viewDidAppear)中完成了所有子视图设置,所以我不明白为什么它没有及时初始化。

有什么建议么?

我还注意到(在调试之后) viewWillAppear 在 vi​​ewDidLoad 方法之前被调用?为什么会这样做?

4

1 回答 1

0

移动以下行:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8f];
[UIView setAnimationTransition:transition forView:self.view cache:YES];

..就在这条线的下方:

[[self view] addSubview:[targetViewController view]];

显然 addSubView/insertSubView 不能在动画中。

于 2011-05-22T21:41:59.130 回答