[iOS]画面遷移(コンテナViewController) | Cocoa練習帳

[iOS]画面遷移(コンテナViewController)

以前のサンプルと比較しやすくするため、ビューを使った画面遷移のサンプルコードをビューコントローラのコンテナ機能を使った物に変更してみた。




TransitionViewのAppDelegate.mを以下の内容に変更。




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    CGRect  screenBounds = [[UIScreen mainScreen] applicationFrame];
    CGRect  windowBounds = [[UIScreen mainScreen] bounds];
     
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.containerViewController = [[UIViewController alloc] init];
    self.containerViewController.view.backgroundColor = [UIColor yellowColor];
    self.myViewController1 = [[MyViewController alloc] init];
    self.myViewController1.view.backgroundColor = [UIColor redColor];
    self.myViewController1.title = @"one";
    self.myViewController2 = [[MyViewController alloc] init];
    self.myViewController2.view.backgroundColor = [UIColor blueColor];
    self.myViewController2.title = @"two";
    
    self.window.rootViewController = self.containerViewController;
    
    /* コンテナViewControllerの子ViewControllerに登録 */
    [self.containerViewController addChildViewController:self.myViewController1];
    [self.containerViewController addChildViewController:self.myViewController2];
    
    /* 強制的に呼ぶ */
    [self.myViewController1 didMoveToParentViewController:self.containerViewController];
    [self.myViewController2 didMoveToParentViewController:self.containerViewController];
    
    /* 最初の画面を設定 */
    self.isView1 = YES;
    CGRect  frame = self.myViewController1.view.frame;
    frame.origin = CGPointMake(0.0, 0.0);
    self.myViewController1.view.frame = frame;
    self.myViewController2.view.frame = frame;
    [self.containerViewController.view addSubview:self.self.myViewController1.view];
    
    [self.window makeKeyAndVisible];
    return YES;
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.isView1) {
        self.isView1 = NO;
        [self.containerViewController transitionFromViewController:self.myViewController1
                                                  toViewController:self.myViewController2
                                                          duration:1.0
                                                           options:UIViewAnimationOptionTransitionCrossDissolve
                                                        animations:NULL
                                                        completion:NULL];
    }
    else {
        self.isView1 = YES;
        [self.containerViewController transitionFromViewController:self.myViewController2
                                                  toViewController:self.myViewController1
                                                          duration:1.0
                                                           options:UIViewAnimationOptionTransitionCrossDissolve
                                                        animations:NULL
                                                        completion:NULL];
    }
}



ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/ios/TransitionView - GitHub

https://github.com/murakami/workbook/tree/master/ios/ContainerVC - GitHub



関連情報
iOS View プログラミングガイド



【Cocoa練習帳】
http://www.bitz.co.jp/weblog/

http://ameblo.jp/bitz/(ミラー・サイト)