UIView在旋转前没有正确初始化

时间:2010-07-31 19:01:59

标签: iphone animation uiviewcontroller transition flip

我使用的代码非常类似于下面:

- (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)中进行了所有子视图设置,所以我不明白为什么它没有及时初始化。

有什么建议吗?

我还注意到(在调试之后)在viewDidLoad方法之前调用viewWillAppear?为什么要这样做?

1 个答案:

答案 0 :(得分: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不能在动画中。