Cocos2d中从场景切换到UIViewController视图方法总结

 第一种:直接从场景切换到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];

posted on 2012-02-24 00:29  疯子123  阅读(122)  评论(0编辑  收藏  举报

导航