在横向推出iPad应用程序时无法识别点击和手势

时间:2013-12-20 19:47:49

标签: ios objective-c ipad uiview cocos2d-iphone

当我在横向模式下在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:方法,当我点击屏幕左侧的死区时,它根本就没有被调用。这是否表明问题可能存在?

这是我创建窗口的app委托:

- (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;
}

这是UIViewController的viewDidLoad方法,其中配置了UIView:

- (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];

这让我发疯;任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

我能够通过以下步骤解决这个问题:

创建UIWindow,其中[UIScreen mainscreen].bounds为其框架,始终为纵向。我之前根据不正确的方向更改了窗口边界。

使用当前状态栏方向确定纵向/横向并相应地设置最低级别UIView的框架。 (即风景为1024x768,肖像为768x1024)

将glView的框架设置为其父UIView的边界,而不是窗口的边界