3

从纵向开始时它工作得非常好,当您从纵向旋转到横向并返回时也可以工作。

在横向启动时它不起作用。但是,当您从横向旋转到纵向并返回时,它会起作用。

在横向启动模式下,屏幕坐标 X 大于 768 时屏幕不会响应任何触摸。

代码中发生的是,我使用状态栏方向来确定原始方向并手动旋转每个视图。视图正确显示,但未正确接收触摸。

然后当 ipad 开始旋转时,我的根视图控制器将被调用:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

这将旋转每个子视图。

根控制器:

- (void)loadView {
    self.view = [[UIView alloc]init ]; 
    //initialize child views
    [self willRotateToInterfaceOrientation:0 duration:0];    

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    if ([model isLandscape]) {
        self.view.frame = CGRectMake(0, 0, 1024, 768-80);
    }
    else {
        self.view.frame = CGRectMake(0, 0, 768, 1024-80);
    }
    //rotate child views  
}

我的代码 [model isLandscape] 有效,因此我不需要提供有关其工作原理的详细信息,但无论如何这里是代码:

- (bool)isLandscape {
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) 
        return true;
    else 
        return false;
}

-(id) init
{
    [[NSNotificationCenter defaultCenter] addObserver:self   selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)orientationChanged:(NSNotification *)notification
{
    UIInterfaceOrientation curOrientation = [[UIDevice currentDevice] orientation];
    if (curOrientation == UIDeviceOrientationPortrait ||
        curOrientation == UIDeviceOrientationPortraitUpsideDown ||
        curOrientation == UIDeviceOrientationLandscapeLeft ||
        curOrientation == UIDeviceOrientationLandscapeRight)
    {
        orientation = curOrientation;
        ((AppDelegate*)([UIApplication sharedApplication].delegate)).savedOrientationForRestart = orientation;
        NSLog(@"changed");
    }
}

-(void)validateOrientation { //first time when initializing orientation
    UIInterfaceOrientation curOrientation = [[UIDevice currentDevice] orientation];
    if (curOrientation != UIDeviceOrientationPortrait &&
        curOrientation != UIDeviceOrientationPortraitUpsideDown &&
        curOrientation != UIDeviceOrientationLandscapeLeft &&
        curOrientation != UIDeviceOrientationLandscapeRight)
    {
        orientation = [[UIApplication sharedApplication] statusBarOrientation];
    }

}
4

1 回答 1

0

好的。所以事实证明,在数十个背景和图层中,有些没有正确旋转到横向/纵向……调试这个问题非常困难……

最后,我连接了一个触摸调试器,看看哪些视图正确接收触摸,哪些没有......

非常有帮助!只需在代码中使用下面的 UIWindow 子类而不是 UIWindow 就可以了。

    #import <UIKit/UIKit.h>
    @interface MyUIWindow : UIWindow {
    }

    -(void)rotate;

    @end

    #import "MyUIWindow.h"
    @implementation MyUIWindow

    -(id) init{
        self = [super init];
        if(self)
        {
        }
        return self;
    }

    - (void)sendEvent:(UIEvent *)event {
        [super sendEvent:event];
        NSSet *touches = [event allTouches];
        if (touches.count != 1)
            return;
        UITouch *touch = touches.anyObject;    
        UIView * view = touch.view;
        NSLog(NSStringFromClass([touch.view class])); //this prints out touch view
    }


    @end
于 2012-12-11T17:02:18.187 回答