15

请考虑下面的代码,并告诉我我做错了什么。

我想在两个 UIView 之间切换。

不知何故,当我从初始视图翻转时,我只是得到翻转的视图,没有动画。当我向后翻转时,动画显示得很好。

翻转是从视图本身的按钮触发的。

- (IBAction)showMoreInfo:(id)sender
{
    UIView *moreInfo = self.flipView;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

    UIView *parent = self.view.superview;
    [self.view removeFromSuperview];

    [parent addSubview:moreInfo];

    [UIView commitAnimations];

}



- (IBAction)showLessInfo:(id)sender
{
    UIView *lessInfo = self.view;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.flipView cache:YES];

    UIView *parent = self.flipView.superview;
    [self.flipView removeFromSuperview];

    [parent addSubview:lessInfo];

    [UIView commitAnimations];

}
4

3 回答 3

17

这可能是因为您没有使用容器视图作为过渡视图。请参阅有关setAnimationTransition:forView:cache 的文档:

如果你想在过渡期间改变视图的外观——例如,从一个视图翻转到另一个——然后使用容器视图,即 UIView 的一个实例,如下所示:

  1. 开始一个动画块。
  2. 在容器视图上设置过渡。
  3. 从容器视图中移除子视图。
  4. 将新的子视图添加到容器视图。
  5. 提交动画块。

尝试self.view.superview在动画过渡视图中使用showMoreInfo:

showLessInfo:方法有效的原因是您使用的是容器视图。

于 2009-05-09T16:11:41.667 回答
12

你可以使用你的 MainWindow (UIWindow) 作为 UIView 继承的 UIWindow 的容器视图吗?

iPhone 3.0 还通过 presentModalViewController 方法引入了翻转事务:

CustomViewController *vc = [[CustomViewController alloc]
    initWithNibName:@"CustomViewController" bundle:nil];

vc.delegate = self;

// The magic statement. This will flip from right to left.
// present the modal view controller then when you dismissModalViewController
// it will transition flip from left to right. Simple and elegant.
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentModalViewController:vc animated:YES];

[vc release];
于 2010-01-21T00:29:45.757 回答
0

在 iOS 4.0 之后,您可以通过以下方式在视图之间切换:

[UIView transitionFromView:sourceView toView:destinationView duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
    NSLog(@"I just flipped!");
}];

正如 Jason 所提到的,您需要在容器视图中执行此操作。

于 2013-08-07T21:39:08.210 回答