Cocos2d中從情境切換到UIViewController視圖方法總結

來源:互聯網
上載者:User

 第一種:直接從情境切換到UIViewController視圖(網上流傳的版本)
- (void) showUIViewController:(UIViewController *) controller
{
    [[Director sharedDirector] pause];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[[Director sharedDirector] openGLView] cache:YES];
    
    [[[Director sharedDirector] openGLView] addSubview:controller.view];
    
    [UIView commitAnimations];
}

從UIViewController視圖切換到情境
//返回情境視圖
- (void) hideUIViewController:(UIViewController *) controller
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animDone:finished:context:)];
    
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:[[Director sharedDirector] openGLView] cache:YES];
    
    [controller.view removeFromSuperview];
    
    [UIView commitAnimations];
}

-(void)animDone:(NSString*) animationID finished:(BOOL) finished context:(void*) context
{    
    [[Director sharedDirector] resume];
} 備忘一點:代碼1:[[[[CCDirector sharedDirector] openGLView] window]addSubview:viewController.view];
效果:添加的view會隨著window是橫屏還是豎屏變化,添加之後如果view下面覆蓋了一個cocos2d的按鈕,點擊按鈕的地區按鈕不響應點擊。
代碼2:[[[CCDirector sharedDirector] openGLView]addSubview:viewController.view];
效果:不隨著變化,並且底部的按鈕相應點擊 第二種:通過RootViewController切換 (自己寫的) 1、預設情況下面,cocos2d 模板並沒有在AppDelegate裡麵包含一個RootViewController的屬性,因此必須手動添加一個。跳轉到AppDelegate.h檔案,並添加下面的代碼:@property (nonatomic, retain) RootViewController *viewController;

然後跳轉到AppDelegate.m,synthesize之:

@synthesize viewController;

2、添加

#import "AppDelegate.h"

- (void) showUIViewController:(UIViewController *) controller
{
    AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.viewController presentModalViewController:controller animated:YES];
    
}

從UIViewController視圖切換到情境
//返回情境視圖

第一種:在UIViewController調用dismissModalViewControllerAnimated 來返回,例如:

[self dismissModalViewControllerAnimated:YES];

第二種使用委託,讓RootViewController來控制,例如

[appdelegate.viewController dismissModalViewControllerAnimated:YES];轉載來自:http://blog.csdn.net/wangqiuyun/article/details/7289104

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.