8

我有一个视图控制器,它在其视图的给定区域显示 2 个子视图控制器的视图。2 个子视图控制器是 FlopVC 和 FipVC。

我想为从一个子视图到另一个子视图的过渡设置动画。我正在使用的代码是:

-(IBAction)flip:(id)sender{

    UIViewController *newVC = nil;

    if (self.isFlip) {
        newVC = [[FlopVC alloc] initWithNibName:nil bundle:nil];
    }else{
        newVC = [[FipVC alloc] initWithNibName:nil bundle:nil];
    }

    newVC.view.frame = CGRectMake(120, 20, 240, 260);
    [self.view addSubview:newVC.view];

    [UIView transitionFromView:self.currentVC.view
                        toView:newVC.view
                      duration:0.9
                       options:UIViewAnimationTransitionFlipFromLeft 
                    completion:^(BOOL finished) {
                        self.currentVC = newVC;
                        self.isFlip = ! self.isFlip;
                    }];



}

子视图被交换,但没有任何动画。我究竟做错了什么?

PS 完整的项目在这里

4

3 回答 3

19
UIView Animation TransitionFlipFromLeft != UIView AnimationOption TransitionFlipFromLeft
于 2011-10-29T16:26:00.813 回答
3

如果您使用的是新的 iOS5 视图容器范例,您需要按照以下方式进行操作:

-(IBAction)flip:(id)sender{

    UIViewController *newVC = nil;

    if (self.isFlip) {
       newVC = [[FlopVC alloc] initWithNibName:nil bundle:nil];
    }else{
       newVC = [[FipVC alloc] initWithNibName:nil bundle:nil];
    }

    newVC.view.frame = CGRectMake(120, 20, 240, 260);

    // required for the new viewController container
    self.currentVC willMoveToParentViewController:nil];
    [self addChildViewController:newVC];
    [self transitionFromViewController:self.currentVC
                  toViewViewController:newVC.view
                              duration:0.9
                               options: UIViewAnimationOptionTransitionFlipFromLeft 
                            animations:nil
                            completion:^(BOOL finished) {
                               // required for the new viewController container
                               [self.currentVC removeFromParentViewController];
                               [newVC didMoveToParentViewController:self];

                               self.currentVC=newVC;
                             }];



}

参考实现容器视图控制器部分和 UIViewController 容器上的 2011 WWDC 视频以获取更多信息。

于 2011-10-28T23:04:57.773 回答
2

这是工作代码(纯属巧合)完全符合您的描述。我的两个孩子 vc 存储在self->swappers. 该整数cur跟踪当前显示的是哪一个。我的界面中子视图所在的位置由视图出口标记,panel.

UIViewController* fromvc = [self->swappers objectAtIndex:cur];
cur = (cur == 0) ? 1 : 0;
UIViewController* tovc = [self->swappers objectAtIndex:cur];
tovc.view.frame = self.panel.bounds;

// must have both as children before we can transition between them
[self addChildViewController:tovc]; // "will" called for us
// note: when we call remove, we must call "will" (with nil) beforehand
[fromvc willMoveToParentViewController:nil];

[self transitionFromViewController:fromvc
                  toViewController:tovc
                          duration:0.4
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:nil
                        completion:^(BOOL done){
                            // note: when we call add, we must call "did" afterwards
                            [tovc didMoveToParentViewController:self];
                            [fromvc removeFromParentViewController]; // "did" called for us
                        }];
于 2011-11-05T02:13:53.020 回答