当我以横向模式在iPad上启动通用应用程序时,该应用程序将无法识别屏幕上最靠近主页按钮的侧面上的轻拍或手势。我已经遍历了所有关于stackoverflow的类似问题,以试图弄清这一点,但是我无法使其正常工作。
看来,视图层次结构中的一个视图是纵向的,而不是横向的,但是我找不到尺寸不正确的任何视图。另外,请注意,该应用在以纵向模式启动时可以正常运行,并且可以正确旋转,并且旋转后可以识别所有轻拍。但是,当在横向模式下启动时,屏幕的一侧始终会存在一个“死区”,无法识别水龙头。
视图层次结构如下所示:
UIWindow (frame: 0, 0, 1024, 768)
UINavigationController
UIViewController
UIView (frame: 0, 0, 1024, 768)
GLView (frame: 0, 0, 1024, 768)
我检查了视图的框架和边界,设置clipsToBounds = YES
并更改了它们的背景颜色,以查看它们的布局是否正确,但它们是否都按预期占据了整个屏幕(1024x768)。
我继承UIWindow
并覆盖了该sendEvent:
方法,当我点击屏幕左侧的盲区时,根本没有调用它。这是否表明问题可能出在哪里?
这是创建窗口的我的应用程序委托:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create the main window
CGRect windowFrame = [[UIScreen mainScreen] bounds];
if (application.statusBarOrientation == UIInterfaceOrientationLandscapeLeft || application.statusBarOrientation == UIInterfaceOrientationLandscapeRight) {
windowFrame = CGRectMake(0, 0, windowFrame.size.height, windowFrame.size.width);
}
window_ = [[MyUIWindow alloc] initWithFrame:windowFrame];
navController_ = [[BFNavigationController alloc] init];
navController_.navigationBarHidden = YES;
// for rotation and other messages
[[CCDirector sharedDirector] setDelegate:navController_];
// set the Navigation Controller as the root view controller
[window_ setRootViewController:navController_];
bfViewController_ = [[BFViewController alloc] init];
[navController_ pushViewController:bfViewController_ animated:NO];
// make main window visible
[window_ makeKeyAndVisible];
return YES;
}
这是配置UIView的UIViewController的viewDidLoad方法:
- (void)viewDidLoad
{
[super viewDidLoad];
UIWindow *mainWindow = (UIWindow*)[UIApplication sharedApplication].windows.firstObject;
self.view.frame = mainWindow.bounds;
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view.autoresizesSubviews = YES;
self.view.clipsToBounds = YES;
[self initCocos2D];
}
这是创建GLView的位置:
- (void)initCocos2D {
UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
CCDirector *director = (CCDirectorIOS*) [CCDirector sharedDirector];
CCGLView *glView = [CCGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
glView.clipsToBounds = YES;
[glView setMultipleTouchEnabled:YES];
director.wantsFullScreenLayout = YES;
// set FPS at 60
[director setAnimationInterval:1.0/60];
// attach the openglView to the director
[director setView:glView];
// 2D projection
[director setProjection:kCCDirectorProjection2D];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change this setting at any time.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// Assume that PVR images have premultiplied alpha
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
[self.view insertSubview:glView atIndex:0];
[[CCDirector sharedDirector] startAnimation];
}
手势识别器已添加到glView中,并由cocos2D游戏层处理:
UIPanGestureRecognizer *panGestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePan:)] autorelease];
[[CCDirector sharedDirector].view addGestureRecognizer:panGestureRecognizer];
UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:layer action:@selector(handleTap:)] autorelease];
[[CCDirector sharedDirector].view addGestureRecognizer:tapGestureRecognizer];
UIPinchGestureRecognizer *pinchGestureRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePinch:)] autorelease];
[[CCDirector sharedDirector].view addGestureRecognizer:pinchGestureRecognizer];
这真让我抓狂; 任何帮助将非常感激。
我可以通过以下步骤解决此问题:
创建UIWindow[UIScreen mainscreen].bounds
为其框架,该框架将始终为纵向。我以前是根据方向更改窗口边界的,这是不正确的。
使用当前状态栏方向确定纵向/横向,并相应地设置最低级别的UIView框架。(即,横向使用1024x768,纵向使用768x1024)
将glView的框架设置为其父UIView的边界,而不是窗口的边界
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句