接下來,就來教這個做法:
使用code/lib:XCode 4、cocos2D 1.0.1
首先,先停止機器自己旋轉,在RootViewController.m下找到該function且改return值:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
再來是重頭戲,請到HelloWorld.m或是其他你要做這個功能的.m檔案中,在初始化的時候加上:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
之後再來添加orientationChanged:這個function即可,該function的寫法在下方<範例>中。
範例:
-(id)init{
sp = [[CCSprite alloc] initWithFile:@"Play_Chess_B1.png"];
sp.position = ccp(240,160);
[self addChild:sp];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
return self;
}
-(void) orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//[UIApplication sharedApplication].statusBarOrientation
CGFloat radian = 0;
switch (orientation) {
case UIInterfaceOrientationPortrait:
radian = -90;
NSLog(@"轉啦!正常直向");
break;
case UIInterfaceOrientationPortraitUpsideDown:
radian = 90;
NSLog(@"轉啦!反直向");
break;
case UIInterfaceOrientationLandscapeLeft:
radian = 180;
NSLog(@"轉啦!按鈕左邊橫向");
break;
case UIInterfaceOrientationLandscapeRight:
radian = 0;
NSLog(@"轉啦!按鈕右邊橫向");
break;
default:
break;
}
sp.rotation = radian;
}
沒有留言:
張貼留言
歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT