0

我正在尝试使用 UIScrollView (pagingEnabled) 制作一个应该适应设备界面方向的应用程序。

为此,我制作了两个加载了 UIImageviews 和 UIButtons 的 UIScrollViews 和动画 (CA)。一个用于纵向,另一个用于横向(您将在我的代码中看到它)。

现在我的问题是每当我旋转设备时,是的!它响应方向变化,但它会回到第一页。

好的图片这个...您已经在纵向滚动视图的第 10 页上,然后当您切换到横向时,它又在第一页上。

我希望解释清楚。

并且...当我旋转它时它在 iPad 上崩溃。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){
        //change the view and subview frames for the portrait view
        [self forPortrait];
        [self removeMainSVLContents];
    }
    else{   
        //change the view and subview  frames for the landscape view
        [self forLandscape];
        [self removeMainSVPContents];
    }

    [UIView commitAnimations];
}

“forPortrait”中的内容与“forLandscape”中的内容相同,它们只是有不同的框架,并且它也有不同的图像,可以适应纵向和横向的屏幕。

4

1 回答 1

0

在动画使用**scrollRectToVisible:animated:**方法显示或停留在滚动视图的期望位置时,这可能对您有用。

于 2012-04-30T09:00:15.663 回答