2011-04-24 74 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 

}