在didRotateFromInterfaceOrientation之后触及了

时间:2011-07-01 16:04:11

标签: iphone ipad uiview uikit

我注意到,在从纵向到横向的方向更改后,我不再为我视图的某些部分进行触摸开始事件了。我想这是因为我没有告知我的UIView关于设备旋转后窗口框架的尺寸变化。

我正在以编程方式设置所有内容(UIWindow,UIViewController,UIView),如下所示:

myViewController = [[myUIViewController alloc] init];
myWindow = [[UIWindow alloc] initWithFrame: rect];
myView = [[myUIView alloc] initWithFrame: [myWindow bounds]];

[myViewController setView:myView];
[myWindow addSubview:myView];
[myWindow setFrame:rect];
[myWindow makeKeyAndVisible];

当我收到didRotateFromInterfaceOrientation通知时,我正在更新这样的窗口框架:

[[[self view] window] setFrame:rect];

但在那之后,我的UIView不再为所有区域获取touchesXXX事件。似乎只有前一帧的区域仍在报告事件。所以我的问题是:在didRotateFromInterfaceOrientation中还有什么需要做的,以告知我的UIView有关尺寸变化的信息吗?

感谢您的帮助!

编辑:我是否必须在didRotateFromInterfaceOrientation()上重新定位UIView,还是自动完成?我注意到当方向改变时,我的UIView的“transform”属性被设置为变换矩阵。但是,这使我很难重新定位我的视图。文档说当转换处于活动状态时不能使用“frame”属性,因此我尝试修改“center”属性以重新定位我的视图,但这也无法正常工作。我想将视图移动到左上角,所以我将“center”设置为(screenwidth / 2,screenheight / 2),但它没有正确定位视图:(任何想法或信息必须要做什么才能获得在定向模式下的事件?

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我认为这是一个框架问题。

我必须根据方向手动设置rects,并在主视图上设置userInteractionEnabled,如:

if (appOrientation == 0)
    appOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (appOrientation == UIInterfaceOrientationLandscapeLeft || appOrientation == UIInterfaceOrientationLandscapeRight) {

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    myView.frame = CGRectMake(0, 0, 1024, 768);

} else {

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    myView.frame = CGRectMake(0, 0, 768, 1024);
}

myView.userInteractionEnabled = YES;

答案 1 :(得分:0)

好的我知道这是一个老问题,但这就是我解决这个问题的方法。

在我的情况下,我有一个故事板,其视图将以纵向显示或强制显示为横向模式,具体取决于用户设置。

我的应用程序始终显示statusBar,除非我显示此视图。

为了使这一切工作,必须在viewWillAppear方法中应用转换。我首先在viewDidAppear方法中使用了以下代码,并且我猜想了touchesBegan事件的边界。

这是viewWillAppear代码:

- (void)viewWillAppear:(BOOL)animated
{
    // Hide the status bar
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    // This is to offset the frame so that the view will take the fullscreen once the status bar is hidden
    self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);

    // If view is to be displayed in landscape mode
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"orientation"])
    {
        // Change status bar orientation
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft];

        // Turn the view 90 degrees
        [self.navigationController.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];

        // Set the frame and the bounds for the view
        // Please Note: Frame size has to be reversed for some reason.
        [self.navigationController.view setFrame: CGRectMake(0,0,320,480)];
        [self.navigationController.view setBounds: CGRectMake(0,0,480,320)];

        // Make sure user can interact with view
        [self.navigationController.view setUserInteractionEnabled:YES];
    }
}

任何其他必须在布局方面发生的事情都必须在viewDidAppear方法中发生。例如,我有一个覆盖整个视图的图像,框架必须根据方向设置。在viewWillAppear中设置框架给出了奇怪的结果,但是相同的代码在viewDidAppear中完美运行。

希望这可以帮助某人,因为我在这件事上撞了6个小时。

相关问题