2011-04-24 72 views
0

我有一个应用程序,它有一个启动屏幕上的开始按钮。如果您点击该按钮,视图会改变并出现一个新视图,那么我们称之为主视图。如何实现UIView转换

我实现它的方式是每个视图一个视图控制器。现在我想添加转换动画,这个实现会导致问题。例如,我想要使用以下代码:

[UIView transitionFromView:startView toView: mainView duration: 2.0 
options: UIViewAnimationOptionTransitionCurlDown completion: NULL]; 

但是这不起任何作用。阅读文档表明,这只有在视图控制器没有改变的情况下才有效。文档中没有任何视图控制器发生变化的卷曲下移过渡的例子,所以我不知道该怎么做。

当我开始编写应用程序时,我阅读了指南,他们建议每个视图有一个视图控制器是正在做的事情的正确方法。

现在当视图控制器发生变化时,实现视图转换的正确方法是什么?非常感谢您的帮助!

编辑:我正在寻找一个解决方案,第一个视图控制器被销毁。

回答

1

Okey-doke,我发现了一个完美的方式来实现我想要的。此代码,在我用新换旧的视图控制器和视图的方法添加的,伟大的工程:

GameViewController *game = [[GameViewController alloc] init]; 
UIView *currentView = [currentViewController view]; 
// remove the current view and replace with myView1 
[currentView removeFromSuperview]; 
// set up an animation for the transition between the views 
CATransition *animation = [CATransition animation]; 
[animation setDuration:0.5]; 
[animation setType:kCATransitionPush]; 
[animation setSubtype:kCATransitionFromBottom]; 
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
[[window layer] addAnimation:animation forKey:@"SwitchToView1"];  
[self setCurrentViewController: game]; 
[window addSubview:[currentViewController view]]; 
[game release]; 

非常感谢HERE

+0

如果任何人从设计的角度看这个问题请在这里发布,我会接受它作为我的问题的正确答案。非常感谢您的帮助! – 2011-04-25 12:31:56

+1

从设计的角度来看,这确实不是一个好主意。请参阅下面的答案。 – Rob 2012-07-07 16:38:25

2

建议您有效地创建视图控制器,使用其视图,但忽略控制器本身。这不建议。在iOS中,您希望保持视图控制器层次结构与视图层次结构同步。因此,所提出的任何技术都是有问题的。 (WWDC 2011 session 102,虽然在不同的主题,视图控制器遏制,有一个冗长的讨论,保持这两个层次同步的重要性)。

最明显的问题是,某些事件,特别是旋转事件,不会传输成功地到你的新控制器,你可以得到一些非常奇怪的结果。上面的代码也不清楚你以前的旧视图会发生什么情况,你将如何转换回来,如何清理现在不必要的游戏控制器,如果你在游戏控制器中,会发生在didReceiveMemoryWarning上,等等这似乎只是带来了很多问题。

最简单的解决方案是将标准pushViewController和/或presentViewController调用从一个视图转到另一个视图,并且仅对这些转换进行动画处理。如果您对这些方法有任何疑问,或者如何在这些方法的背景下进行动画制作,请告诉我。

+0

谢谢!我会稍微详细地阅读它。 – 2012-07-07 16:42:01

2

顺便问一下,你的问题的根源是如何在视图控制器之间转换。这是我曾经玩过的一个旧的转换集合(一些用于push转换,一些用于模式转换)。也许这里有些东西可以使用?

- (void)transitionToNewController 
{ 
    MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil]; 

#if TRANSITIONTYPE == 1 

    // pushViewController with push/slide from right via CATransition 

    CATransition* transition = [CATransition animation]; 

    transition.duration = 0.3; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromRight; 

    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; 
    [self.navigationController pushViewController:controller animated:NO]; 
#endif 

#if TRANSITIONTYPE == 2 

    // pushViewController with flip using UIView begin/commitAnimations 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:0.75]; 
    [self.navigationController pushViewController:controller animated:NO]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
    [UIView commitAnimations]; 
#endif 

#if TRANSITIONTYPE == 3 

    // pushViewController with flip using animation block 

    [UIView animateWithDuration:0.5 
        animations:^{ 
         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
         [self.navigationController pushViewController:controller animated:NO]; 
         [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
        } 
        completion:nil]; 

#endif 

#if TRANSITIONTYPE == 4 

    // pushViewController with fade via CATransition 

    CATransition* transition = [CATransition animation]; 

    transition.duration = 0.5; 
    transition.type = kCATransitionFade; 

    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; 
    [self.navigationController pushViewController:controller animated:NO]; 
#endif 

#if TRANSITIONTYPE == 5 

    // pushViewController with fade via animation block 

    controller.view.alpha = 0.0f; 

    [UIView animateWithDuration:0.5 
        animations:^{ 
         self.view.alpha = 0.0f; 
         controller.view.alpha = 1.0f; 
        } 
        completion:^(BOOL finished){ 
         [self.navigationController pushViewController:controller animated:NO]; 
         self.view.alpha = 1.0f; // reset the src alpha so it's there when you pop your view controller off 
        }]; 
#endif 

#if TRANSITIONTYPE == 6 

    // pushViewController Flip using UIView begin/commitAnimations 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:0.75]; 
    [self.navigationController pushViewController:controller animated:NO]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self.self navigationController].view cache:NO]; 
    [UIView commitAnimations]; 
#endif 

#if TRANSITIONTYPE == 7 

    // pushViewController animation using CGAffine scale to look like we're zooming into the new view 

    [self.view addSubview:controller.view]; 
    [controller.view setFrame:self.view.window.frame]; 
    [controller.view setTransform:CGAffineTransformMakeScale(0.5,0.5)]; 
    [controller.view setAlpha:1.0]; 

    [UIView animateWithDuration:0.5 
          delay:0.0 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
         [controller.view setTransform:CGAffineTransformMakeScale(1.0,1.0)]; 
         [controller.view setAlpha:1.0]; 
        } 
        completion:^(BOOL finished){ 
         [controller.view removeFromSuperview]; 
         [self.navigationController pushViewController:controller animated:NO]; 
        }]; 
#endif 

#if TRANSITIONTYPE == 8 

    // presentViewController animation with slide from right by using CGAffineTransform 

    self.view.transform = CGAffineTransformMakeTranslation(0, 0); 
    controller.view.transform = CGAffineTransformMakeTranslation(self.view.frame.size.width, 0); 
    [self.view addSubview:controller.view]; 

    [UIView animateWithDuration:4.0 
        animations:^{ 
         //       [self presentViewController:controller animated:NO completion:nil]; 
         self.view.transform = CGAffineTransformMakeTranslation(self.view.frame.size.width, 0); 
         controller.view.transform = CGAffineTransformMakeTranslation(0, 0); 
        } 
        completion:^(BOOL finished){ 
         [controller.view removeFromSuperview]; 
         [self presentViewController:controller animated:NO completion:nil]; 
        } 
    ]; 
#endif 

#if TRANSITIONTYPE == 9 

    // presentViewController animation with slide from right by just animating frames 

    float width = self.navigationController.view.frame.size.width; 

    CGPoint right = controller.view.center; 
    right.x += width; 
    controller.view.center = right; 

    [self.navigationController.view addSubview:controller.view]; 

    [UIView animateWithDuration:0.5 
        animations:^{ 
         CGPoint left = self.navigationController.view.center; 
         left.x -= width; 
         self.navigationController.view.center = left; 
        } 
        completion:^(BOOL finished){ 
         [controller.view removeFromSuperview]; 
         [self presentViewController:controller animated:NO completion:nil]; 
        } 
    ]; 
#endif 

#if TRANSITIONTYPE == 10 

    // presentViewController animation with flipfromright 

    [UIView animateWithDuration:4.0 
          delay:0.0 
         options:UIViewAnimationOptionTransitionFlipFromRight 
        animations:^{ 
         [self.view addSubview:controller.view]; 
        } 
        completion:^(BOOL finished){ 
         [controller.view removeFromSuperview]; 
         [self presentViewController:controller animated:NO completion:nil]; 
        }]; 
#endif 

#if TRANSITIONTYPE == 11 

    // presentViewController with flip using UIView begin/commitAnimations 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:0.75]; 
    [self presentViewController:controller animated:NO completion:nil]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO]; 
    [UIView commitAnimations]; 
#endif 

#if TRANSITIONTYPE == 13 

    // pushViewController with flip using animation block with transitionWithView 

    controller.view.frame = self.view.window.frame; 

    [UIView transitionWithView:self.view.superview 
         duration:1.0 
         options:UIViewAnimationTransitionFlipFromRight 
        animations:^{ 
         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
         [self.view.superview addSubview:controller.view]; 
         [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
        } 
        completion:^(BOOL finished){ 
         [self.navigationController pushViewController:controller 
                  animated:NO]; 
        }]; 

#endif 

}