如何阻止mainView旋转?

时间:2011-01-05 17:07:48

标签: iphone objective-c cocoa-touch orientation

我有这个代码,设置为在手机旋转到横向时显示clockView,然后在它返回到肖像时返回主视图。但是当它回到肖像时,纵向视图处于横向模式。我该如何解决?这是我的代码。

-(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;

    if(toOrientation == UIInterfaceOrientationPortrait)
    {
        self.view = mainView;
    }

    else if(toOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view = clockView;
    }

    else if (toOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view = mainView;
    }

    else if(toOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = clockView;
    }
}

1 个答案:

答案 0 :(得分:3)

将mainView和clockView作为视图控制器的子视图可能更好:

// in viewDidLoad do [self.view addSubView:mainView]; [self.view addSubView:clockView]; clockView.hidden = YES;

-(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if(UIInterfaceOrientationIsPortrait(toOrientation)) {
        mainView.hidden = NO;
        clockView.hidden = YES;
    }
    else {
        mainView.hidden = YES;
        clockView.hidden = NO;
    }
}

这样,两个视图都会以正确的方式自动旋转,您的问题就会消失。