我正在继承UIView并覆盖drawRect方法.我注意到只在视图首次加载时才调用视图的drawrect.之后再也不会再打电话了.如何确保在方向更改后调用它?我已经尝试在旋转之前和之后调用setNeedsDisplay,但是没有这样做.
我正在创建一个允许设备处于横向左侧或横向右侧方向的游戏,玩家可以在暂停时更改方向.当他们这样做时,我需要改变游戏根据方向解释加速度计的方式.
在iOS 5中,我使用了willRotateToInterfaceOrientation来捕获更改并更改我的变量,但这在iOS6中已被弃用.我现有的代码如下所示:
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
GameEngine *engine = [GameEngine sharedEngine];
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
engine.orientation = -1;
} else {
engine.orientation = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道替换是UIViewController类中的viewWillLayoutSubviews方法.我正在cocos2d 2.1中构建这个游戏,并且在演示项目中似乎没有UIViewController类,所以我不清楚如何合并它以及代码应该如何使其工作.